Submission #540884

# Submission time Handle Problem Language Result Execution time Memory
540884 2022-03-21T22:39:15 Z skittles1412 Love Polygon (BOI18_polygon) C++17
100 / 100
135 ms 17520 KB
#include "bits/extc++.h"

using namespace std;

template <typename T>
void dbgh(const T& t) {
	cerr << t << endl;
}

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
	cerr << t << " | ";
	dbgh(u...);
}

#ifdef DEBUG
#define dbg(...)                                           \
	cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
		 << ": ";                                          \
	dbgh(__VA_ARGS__)
#else
#define cerr   \
	if (false) \
	cerr
#define dbg(...)
#endif

#define endl "\n"
#define long int64_t
#define sz(x) int((x).size())

int solver(const vector<bool> &arr) {
	int n = sz(arr);
	int dp[n + 1];
	dp[n] = 0;
	for (int i = n - 1; i >= 0; i--) {
		dp[i] = 1 + dp[i + 1];
		if (i + 1 < n) {
			dp[i] = min(dp[i], 1 + arr[i] + arr[i + 1] + dp[i + 2]);
		}
	}
	return dp[0];
}

int solve(vector<bool> arr) {
	if (sz(arr) == 2) {
		return arr[0] + arr[1];
	}
	int ans = solver(arr);
	dbg(sz(arr));
	rotate(arr.begin(), arr.begin() + 1, arr.end());
	return min(ans, solver(arr));
}

void solve() {
	dbg(solve({0, 1, 0}));
	map<string, int> m;
	auto conv = [&](const string &s) -> int {
		static int sind = 0;
		auto [it, inserted] = m.emplace(s, 0);
		if (inserted) {
			return it->second = sind++;
		}
		return it->second;
	};
	int n;
	cin >> n;
	if (n & 1) {
		cout << -1 << endl;
		return;
	}
	int arr[n], edges[n][2];
	for (auto& [u, v] : edges) {
		string s, t;
		cin >> s >> t;
		u = conv(s);
		v = conv(t);
		arr[u] = v;
	}
	bool used[n] {}, cycle[n] {};
	vector<vector<int>> cycles;
	{
		int vis[n] {};
		for (int i = 0; i < n; i++) {
			if (vis[i]) {
				continue;
			} else {
				int j = i;
				vector<int> st;
				while (true) {
					if (vis[j] == 2) {
						break;
					} else if (vis[j] == 1) {
						vector<int> cur(find(begin(st), end(st), j), st.end());
						cycles.push_back(cur);
						for (auto& a : cur) {
							cycle[a] = true;
						}
						break;
					}
					st.push_back(j);
					vis[j] = 1;
					j = arr[j];
				}
				for (auto& a : st) {
					vis[a] = 2;
				}
			}
		}
	}
	int ideg[n] {};
	for (auto& a : arr) {
		ideg[a]++;
	}
	vector<int> q;
	for (int i = 0; i < n; i++) {
		if (!ideg[i]) {
			q.push_back(i);
		}
	}
	int ans = 0;
	while (sz(q)) {
		int u = q.back();
		q.pop_back();
		if (!used[u] && !used[arr[u]] && !cycle[arr[u]] && arr[u] != u) {
			ans++;
			used[u] = used[arr[u]] = true;
			int v = arr[arr[u]];
			if (!(--ideg[v])) {
				q.push_back(v);
			}
		}
	}
	memset(ideg, 0, sizeof(ideg));
	for (int i = 0; i < n; i++) {
		if (!used[i]) {
			ideg[arr[i]]++;
		}
	}
	dbg(ans);
	for (auto& a : cycles) {
		for (auto& b : a) {
			assert(!used[b]);
		}
		dbg(sz(a));
		vector<bool> cur;
		for (auto& b : a) {
			cur.push_back(ideg[b] > 1);
			dbg(b, ideg[b], cur.back());
			ans += max(0, ideg[b] - 2);
		}
		ans += solve(cur);
	}
	for (int i = 0; i < n; i++) {
		if (!used[i] && !cycle[i] && !cycle[arr[i]]) {
			ans++;
		}
	}
	cout << ans << endl;
}

int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cin.exceptions(ios::failbit);
	solve();
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 121 ms 13248 KB Output is correct
5 Correct 135 ms 13336 KB Output is correct
6 Correct 128 ms 13300 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 118 ms 12284 KB Output is correct
2 Correct 125 ms 12164 KB Output is correct
3 Correct 103 ms 17484 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 122 ms 12488 KB Output is correct
6 Correct 112 ms 12360 KB Output is correct
7 Correct 121 ms 12380 KB Output is correct
8 Correct 117 ms 15180 KB Output is correct
9 Correct 111 ms 12760 KB Output is correct
10 Correct 85 ms 12672 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 320 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 121 ms 13248 KB Output is correct
20 Correct 135 ms 13336 KB Output is correct
21 Correct 128 ms 13300 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 118 ms 12284 KB Output is correct
24 Correct 125 ms 12164 KB Output is correct
25 Correct 103 ms 17484 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 122 ms 12488 KB Output is correct
28 Correct 112 ms 12360 KB Output is correct
29 Correct 121 ms 12380 KB Output is correct
30 Correct 117 ms 15180 KB Output is correct
31 Correct 111 ms 12760 KB Output is correct
32 Correct 85 ms 12672 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 116 ms 12108 KB Output is correct
38 Correct 131 ms 12620 KB Output is correct
39 Correct 117 ms 12760 KB Output is correct
40 Correct 112 ms 12416 KB Output is correct
41 Correct 113 ms 12384 KB Output is correct
42 Correct 114 ms 12344 KB Output is correct
43 Correct 122 ms 12444 KB Output is correct
44 Correct 117 ms 12364 KB Output is correct
45 Correct 117 ms 12388 KB Output is correct
46 Correct 116 ms 12432 KB Output is correct
47 Correct 120 ms 12688 KB Output is correct
48 Correct 117 ms 12256 KB Output is correct
49 Correct 116 ms 12164 KB Output is correct
50 Correct 108 ms 17520 KB Output is correct
51 Correct 0 ms 340 KB Output is correct
52 Correct 115 ms 12344 KB Output is correct
53 Correct 123 ms 12368 KB Output is correct
54 Correct 114 ms 12480 KB Output is correct
55 Correct 108 ms 15164 KB Output is correct
56 Correct 108 ms 12624 KB Output is correct
57 Correct 85 ms 12744 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 1 ms 316 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 1 ms 212 KB Output is correct
62 Correct 0 ms 212 KB Output is correct
63 Correct 1 ms 320 KB Output is correct
64 Correct 0 ms 316 KB Output is correct
65 Correct 119 ms 13088 KB Output is correct
66 Correct 128 ms 13348 KB Output is correct
67 Correct 134 ms 13340 KB Output is correct
68 Correct 0 ms 340 KB Output is correct
69 Correct 0 ms 212 KB Output is correct
70 Correct 0 ms 212 KB Output is correct
71 Correct 1 ms 212 KB Output is correct
72 Correct 0 ms 316 KB Output is correct
73 Correct 1 ms 212 KB Output is correct
74 Correct 1 ms 212 KB Output is correct
75 Correct 1 ms 212 KB Output is correct
76 Correct 0 ms 212 KB Output is correct
77 Correct 1 ms 320 KB Output is correct
78 Correct 0 ms 212 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 1 ms 320 KB Output is correct
81 Correct 1 ms 212 KB Output is correct
82 Correct 0 ms 212 KB Output is correct
83 Correct 0 ms 212 KB Output is correct