#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 25;
int p[MAXN], n, deg[MAXN];
map <string, int> d;
bool vis[MAXN];
queue <int> o;
int main () {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
string s, t;
cin >> s >> t;
if (!d.count(s)) d[s] = (int)d.size();
if (!d.count(t)) d[t] = (int)d.size();
p[d[s]] = d[t];
deg[d[t]]++;
}
if (n & 1) {
cout << "-1\n";
return 0;
}
int ans = 0;
for (int i = 0; i < n; i++) if (deg[i] == 0) o.push(i);
while (!o.empty()) {
auto k = o.front(); o.pop();
if (deg[k] != 0) continue;
if (p[p[k]] != p[k] && p[p[p[k]]] == p[k]) continue;
if (p[p[k]] == p[k]) {
p[p[k]] = k;
deg[k]++; deg[p[k]]--;
ans++;
continue;
}
ans++;
deg[p[p[k]]]--;
if (deg[p[p[k]]] == 0) {
o.push(p[p[k]]);
}
p[p[k]] = k; deg[k]++;
}
for (int i = 0; i < n; i++) {
if (deg[i] == 0) {
ans++;
deg[i] = 2;
deg[p[i]]--;
}
}
for (int i = 0; i < n; i++) {
if (deg[i] == 1 && !vis[i]) {
vector <int> t = {i};
vis[i] = 1;
int u = p[i];
while (u != i) {
t.push_back(u); vis[u] = 1;
u = p[u];
}
if (t.size() == 2) continue;
int z = t.size();
ans += (z >> 1) + (z & 1);
}
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
154 ms |
12104 KB |
Output is correct |
5 |
Correct |
144 ms |
11228 KB |
Output is correct |
6 |
Correct |
143 ms |
11832 KB |
Output is correct |
7 |
Correct |
160 ms |
11000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
162 ms |
11232 KB |
Output is correct |
2 |
Correct |
161 ms |
11320 KB |
Output is correct |
3 |
Correct |
128 ms |
11188 KB |
Output is correct |
4 |
Correct |
161 ms |
11332 KB |
Output is correct |
5 |
Correct |
149 ms |
11092 KB |
Output is correct |
6 |
Correct |
142 ms |
11368 KB |
Output is correct |
7 |
Correct |
141 ms |
11348 KB |
Output is correct |
8 |
Correct |
139 ms |
11248 KB |
Output is correct |
9 |
Correct |
139 ms |
11600 KB |
Output is correct |
10 |
Correct |
103 ms |
11600 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
504 KB |
Output is correct |
19 |
Correct |
154 ms |
12104 KB |
Output is correct |
20 |
Correct |
144 ms |
11228 KB |
Output is correct |
21 |
Correct |
143 ms |
11832 KB |
Output is correct |
22 |
Correct |
160 ms |
11000 KB |
Output is correct |
23 |
Correct |
162 ms |
11232 KB |
Output is correct |
24 |
Correct |
161 ms |
11320 KB |
Output is correct |
25 |
Correct |
128 ms |
11188 KB |
Output is correct |
26 |
Correct |
161 ms |
11332 KB |
Output is correct |
27 |
Correct |
149 ms |
11092 KB |
Output is correct |
28 |
Correct |
142 ms |
11368 KB |
Output is correct |
29 |
Correct |
141 ms |
11348 KB |
Output is correct |
30 |
Correct |
139 ms |
11248 KB |
Output is correct |
31 |
Correct |
139 ms |
11600 KB |
Output is correct |
32 |
Correct |
103 ms |
11600 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
147 ms |
11348 KB |
Output is correct |
38 |
Correct |
143 ms |
11376 KB |
Output is correct |
39 |
Correct |
143 ms |
11348 KB |
Output is correct |
40 |
Correct |
146 ms |
11692 KB |
Output is correct |
41 |
Correct |
143 ms |
11344 KB |
Output is correct |
42 |
Correct |
145 ms |
11348 KB |
Output is correct |
43 |
Correct |
169 ms |
11348 KB |
Output is correct |
44 |
Correct |
145 ms |
11348 KB |
Output is correct |
45 |
Correct |
143 ms |
11344 KB |
Output is correct |
46 |
Correct |
145 ms |
11344 KB |
Output is correct |
47 |
Correct |
138 ms |
11364 KB |
Output is correct |
48 |
Correct |
144 ms |
11348 KB |
Output is correct |
49 |
Correct |
145 ms |
11344 KB |
Output is correct |
50 |
Correct |
129 ms |
11088 KB |
Output is correct |
51 |
Correct |
139 ms |
11204 KB |
Output is correct |
52 |
Correct |
152 ms |
11216 KB |
Output is correct |
53 |
Correct |
149 ms |
11344 KB |
Output is correct |
54 |
Correct |
143 ms |
11476 KB |
Output is correct |
55 |
Correct |
144 ms |
11340 KB |
Output is correct |
56 |
Correct |
148 ms |
11428 KB |
Output is correct |
57 |
Correct |
102 ms |
11604 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
348 KB |
Output is correct |
62 |
Correct |
1 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
150 ms |
11812 KB |
Output is correct |
66 |
Correct |
143 ms |
11092 KB |
Output is correct |
67 |
Correct |
158 ms |
12196 KB |
Output is correct |
68 |
Correct |
184 ms |
11024 KB |
Output is correct |
69 |
Correct |
1 ms |
348 KB |
Output is correct |
70 |
Correct |
0 ms |
348 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
344 KB |
Output is correct |
77 |
Correct |
1 ms |
348 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
1 ms |
396 KB |
Output is correct |
80 |
Correct |
0 ms |
472 KB |
Output is correct |
81 |
Correct |
0 ms |
348 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |