Submission #759529

# Submission time Handle Problem Language Result Execution time Memory
759529 2023-06-16T11:22:26 Z MetalPower Love Polygon (BOI18_polygon) C++14
100 / 100
178 ms 12680 KB
#include <bits/stdc++.h>
using namespace std;

const int MX = 2e5 + 10;

struct dsu{
	int p[MX], cc[MX];

	void init(){
		for(int i = 0; i < MX; i++) p[i] = i, cc[i] = 1;
	}

	int f(int x){
		if(p[x] == x) return x;
		else return p[x] = f(p[x]);
	}

	void Join(int u, int v){
		int fu = f(u), fv = f(v);
		if(fu == fv) return;
		p[fu] = fv;
		cc[fv] += cc[fu];
	}
} D;

int N, a[MX], b[MX], nx[MX], degree[MX], vis[MX], tim = 0;
map<string, int> mp;

bool dead[MX];

int dfs(int x){
	if(vis[x]) return 0;
	vis[x] = true;
	return 1 + dfs(nx[x]);
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	int ans = 0;

	cin >> N;
	for(int i = 0; i < N; i++){
		string s, t; cin >> s >> t;
		if(!mp.count(s)) mp[s] = ++tim;
		if(!mp.count(t)) mp[t] = ++tim;
		a[i] = mp[s], b[i] = mp[t];
		nx[a[i]] = b[i];
		degree[b[i]]++;
		if(nx[b[i]] == a[i] && a[i] != b[i]){
			vis[a[i]] = true;
			vis[b[i]] = true;
		}
	}

	if(N % 2 == 1){
		cout << -1 << '\n';
		return 0;
	}

	vector<int> qsolve;
	for(int i = 1; i <= N; i++){
		if(degree[i] == 0) qsolve.push_back(i);
	}

	for(; !qsolve.empty(); ){
		int st = qsolve.back(); qsolve.pop_back();
		if(vis[st] || vis[nx[st]]) continue;
		ans++;
		vis[st] = 1;
		vis[nx[st]] = 1;
		degree[nx[st]]--;
		if(nx[nx[st]] == nx[st]) continue;
		degree[nx[nx[st]]]--;
		if(!vis[nx[nx[st]]] && degree[nx[nx[st]]] == 0) qsolve.push_back(nx[nx[st]]);
	}

	for(int i = 1; i <= N; i++){
		if(vis[i]) continue;
		int curr_sz = dfs(i);
		ans += (curr_sz + 1) / 2;
	}

	cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 135 ms 10000 KB Output is correct
5 Correct 128 ms 10060 KB Output is correct
6 Correct 127 ms 10040 KB Output is correct
7 Correct 178 ms 9732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 135 ms 10336 KB Output is correct
2 Correct 142 ms 12400 KB Output is correct
3 Correct 126 ms 12168 KB Output is correct
4 Correct 138 ms 11812 KB Output is correct
5 Correct 140 ms 12244 KB Output is correct
6 Correct 133 ms 12524 KB Output is correct
7 Correct 137 ms 12552 KB Output is correct
8 Correct 125 ms 12560 KB Output is correct
9 Correct 127 ms 12680 KB Output is correct
10 Correct 105 ms 12396 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 135 ms 10000 KB Output is correct
20 Correct 128 ms 10060 KB Output is correct
21 Correct 127 ms 10040 KB Output is correct
22 Correct 178 ms 9732 KB Output is correct
23 Correct 135 ms 10336 KB Output is correct
24 Correct 142 ms 12400 KB Output is correct
25 Correct 126 ms 12168 KB Output is correct
26 Correct 138 ms 11812 KB Output is correct
27 Correct 140 ms 12244 KB Output is correct
28 Correct 133 ms 12524 KB Output is correct
29 Correct 137 ms 12552 KB Output is correct
30 Correct 125 ms 12560 KB Output is correct
31 Correct 127 ms 12680 KB Output is correct
32 Correct 105 ms 12396 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 344 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 139 ms 12336 KB Output is correct
38 Correct 158 ms 12572 KB Output is correct
39 Correct 148 ms 12592 KB Output is correct
40 Correct 136 ms 12580 KB Output is correct
41 Correct 131 ms 12516 KB Output is correct
42 Correct 142 ms 12600 KB Output is correct
43 Correct 129 ms 12460 KB Output is correct
44 Correct 129 ms 12504 KB Output is correct
45 Correct 159 ms 12544 KB Output is correct
46 Correct 144 ms 12652 KB Output is correct
47 Correct 125 ms 12676 KB Output is correct
48 Correct 132 ms 12432 KB Output is correct
49 Correct 130 ms 12404 KB Output is correct
50 Correct 118 ms 12228 KB Output is correct
51 Correct 130 ms 11856 KB Output is correct
52 Correct 133 ms 12236 KB Output is correct
53 Correct 129 ms 12536 KB Output is correct
54 Correct 131 ms 12516 KB Output is correct
55 Correct 137 ms 12448 KB Output is correct
56 Correct 125 ms 12632 KB Output is correct
57 Correct 99 ms 12344 KB Output is correct
58 Correct 0 ms 340 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 0 ms 340 KB Output is correct
61 Correct 0 ms 336 KB Output is correct
62 Correct 0 ms 340 KB Output is correct
63 Correct 0 ms 340 KB Output is correct
64 Correct 0 ms 340 KB Output is correct
65 Correct 142 ms 12304 KB Output is correct
66 Correct 139 ms 12220 KB Output is correct
67 Correct 129 ms 12184 KB Output is correct
68 Correct 128 ms 11772 KB Output is correct
69 Correct 0 ms 340 KB Output is correct
70 Correct 0 ms 292 KB Output is correct
71 Correct 0 ms 340 KB Output is correct
72 Correct 0 ms 340 KB Output is correct
73 Correct 0 ms 340 KB Output is correct
74 Correct 0 ms 340 KB Output is correct
75 Correct 0 ms 340 KB Output is correct
76 Correct 0 ms 340 KB Output is correct
77 Correct 0 ms 336 KB Output is correct
78 Correct 0 ms 340 KB Output is correct
79 Correct 1 ms 340 KB Output is correct
80 Correct 0 ms 340 KB Output is correct
81 Correct 0 ms 340 KB Output is correct
82 Correct 0 ms 340 KB Output is correct
83 Correct 0 ms 340 KB Output is correct