#include <bits/stdc++.h>
using namespace std;
#define MAX 200007
#define int long long
bitset<MAX> visited;
int curSz = 0;
void dfs(int x, int adj[]){
if (visited[x]) return;
curSz++;
visited[x] = 1;
dfs(adj[x], adj);
}
main(){
int n; cin >> n;
if (n % 2 == 1) {cout << -1; return 0;}
map<string, string> init;
map<string, int> indexed;
int adj[n];
for (int x = 0; x < n; x++){
string a, b; cin >> a >> b;
init[a] = b;
}
string name[n]; int ptr = 0;
for (auto y : init){
name[ptr] = y.first;
indexed[y.first] = ptr;
ptr++;
}
int indeg[n]; memset(indeg, 0, sizeof(indeg));
for (auto y : init){
adj[indexed[y.first]] = indexed[y.second];
if (y.first != y.second) indeg[indexed[y.second]]++;
}
bitset<MAX> taken;
queue<int> process;
for (int x = 0; x < n; x++){
if (indeg[x] == 0) process.push(x);
if (x != adj[x] && adj[adj[x]] == x) taken[x] = 1, taken[adj[x]] = 1;
}
int ans = 0;
while (!process.empty()){
int x = process.front();
process.pop();
if (taken[x]) continue;
if (taken[adj[x]]){
adj[x] = x;
//indeg decrease, but doesn't matter since already taken
continue;
}
ans++;
indeg[adj[adj[x]]]--;
if (indeg[adj[adj[x]]] == 0) process.push(adj[adj[x]]);
adj[adj[x]] = x;
taken[x] = 1;
taken[adj[x]] = 1;
}
for (int x = 0; x < n; x++){
if (!taken[x]){
curSz = 0;
dfs(x, adj);
ans += (curSz+1)/2;
}
}
cout << ans;
}
Compilation message
polygon.cpp:16:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
16 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
315 ms |
26052 KB |
Output is correct |
5 |
Correct |
275 ms |
25940 KB |
Output is correct |
6 |
Correct |
265 ms |
25936 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
254 ms |
23908 KB |
Output is correct |
2 |
Correct |
298 ms |
23908 KB |
Output is correct |
3 |
Correct |
200 ms |
24680 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
288 ms |
23908 KB |
Output is correct |
6 |
Correct |
282 ms |
24164 KB |
Output is correct |
7 |
Correct |
316 ms |
24300 KB |
Output is correct |
8 |
Correct |
274 ms |
24444 KB |
Output is correct |
9 |
Correct |
294 ms |
24680 KB |
Output is correct |
10 |
Correct |
223 ms |
24524 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
315 ms |
26052 KB |
Output is correct |
20 |
Correct |
275 ms |
25940 KB |
Output is correct |
21 |
Correct |
265 ms |
25936 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
254 ms |
23908 KB |
Output is correct |
24 |
Correct |
298 ms |
23908 KB |
Output is correct |
25 |
Correct |
200 ms |
24680 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
288 ms |
23908 KB |
Output is correct |
28 |
Correct |
282 ms |
24164 KB |
Output is correct |
29 |
Correct |
316 ms |
24300 KB |
Output is correct |
30 |
Correct |
274 ms |
24444 KB |
Output is correct |
31 |
Correct |
294 ms |
24680 KB |
Output is correct |
32 |
Correct |
223 ms |
24524 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
279 ms |
25920 KB |
Output is correct |
38 |
Correct |
286 ms |
26328 KB |
Output is correct |
39 |
Correct |
297 ms |
26324 KB |
Output is correct |
40 |
Correct |
258 ms |
26320 KB |
Output is correct |
41 |
Correct |
302 ms |
26308 KB |
Output is correct |
42 |
Correct |
254 ms |
26328 KB |
Output is correct |
43 |
Correct |
277 ms |
26324 KB |
Output is correct |
44 |
Correct |
300 ms |
26388 KB |
Output is correct |
45 |
Correct |
309 ms |
26284 KB |
Output is correct |
46 |
Correct |
228 ms |
26196 KB |
Output is correct |
47 |
Correct |
272 ms |
26440 KB |
Output is correct |
48 |
Correct |
265 ms |
26176 KB |
Output is correct |
49 |
Correct |
311 ms |
26120 KB |
Output is correct |
50 |
Correct |
215 ms |
26836 KB |
Output is correct |
51 |
Correct |
1 ms |
344 KB |
Output is correct |
52 |
Correct |
307 ms |
25940 KB |
Output is correct |
53 |
Correct |
295 ms |
26488 KB |
Output is correct |
54 |
Correct |
267 ms |
26340 KB |
Output is correct |
55 |
Correct |
234 ms |
26444 KB |
Output is correct |
56 |
Correct |
256 ms |
26540 KB |
Output is correct |
57 |
Correct |
203 ms |
26784 KB |
Output is correct |
58 |
Correct |
1 ms |
500 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
1 ms |
600 KB |
Output is correct |
63 |
Correct |
0 ms |
344 KB |
Output is correct |
64 |
Correct |
1 ms |
348 KB |
Output is correct |
65 |
Correct |
244 ms |
25936 KB |
Output is correct |
66 |
Correct |
269 ms |
26184 KB |
Output is correct |
67 |
Correct |
282 ms |
26324 KB |
Output is correct |
68 |
Correct |
1 ms |
472 KB |
Output is correct |
69 |
Correct |
1 ms |
348 KB |
Output is correct |
70 |
Correct |
1 ms |
348 KB |
Output is correct |
71 |
Correct |
1 ms |
348 KB |
Output is correct |
72 |
Correct |
1 ms |
348 KB |
Output is correct |
73 |
Correct |
1 ms |
348 KB |
Output is correct |
74 |
Correct |
1 ms |
348 KB |
Output is correct |
75 |
Correct |
1 ms |
460 KB |
Output is correct |
76 |
Correct |
1 ms |
348 KB |
Output is correct |
77 |
Correct |
1 ms |
348 KB |
Output is correct |
78 |
Correct |
1 ms |
348 KB |
Output is correct |
79 |
Correct |
1 ms |
348 KB |
Output is correct |
80 |
Correct |
1 ms |
348 KB |
Output is correct |
81 |
Correct |
1 ms |
344 KB |
Output is correct |
82 |
Correct |
1 ms |
460 KB |
Output is correct |
83 |
Correct |
1 ms |
348 KB |
Output is correct |