#include <bits/stdc++.h>
std::vector <int> lv;
std::vector <bool> vis;
int dfs(int node) {
vis[node] = true;
return (vis[lv[node]] ? 1 : dfs(lv[node]) + 1);
}
int main() {
std::ios::sync_with_stdio(0);
int n; std::cin >> n;
std::map <std::string, int> mp;
std::string s, t;
int m = 1, ans = 0;
std::vector <int> ld(n + 1, 0);
lv.resize(n + 1, 0);
vis.resize(n + 1, false);
for (int i = 0; i < n; i++) {
std::cin >> s >> t;
if (!mp[s]) mp[s] = m++;
if (!mp[t]) mp[t] = m++;
lv[mp[s]] = mp[t];
ld[mp[t]]++;
}
if (n & 1) {
std::cout << "-1\n";
return 0;
}
std::queue <int> q;
for (int i = 0; i < n; i++) {
if (i + 1 == lv[lv[i + 1]] && lv[i + 1] != i + 1) {
vis[i + 1] = true;
vis[lv[i + 1]] = true;
} else if (!ld[i + 1]) q.push(i + 1);
}
while (q.size()) {
int now = q.front(); q.pop();
vis[now] = true;
if (!vis[lv[now]]) {
vis[lv[now]] = true;
if (!--ld[lv[lv[now]]]) q.push(lv[lv[now]]);
}
ans++;
}
for (int i = 0; i < n; i++)
if (!vis[i + 1]) ans += (dfs(i + 1) + 1) >> 1;
std::cout << ans << "\n";
std::cin >> n;
}
// imagine there's no heaven
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
290 ms |
11128 KB |
Output is correct |
5 |
Correct |
268 ms |
11000 KB |
Output is correct |
6 |
Correct |
277 ms |
11000 KB |
Output is correct |
7 |
Correct |
265 ms |
11256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
263 ms |
9276 KB |
Output is correct |
2 |
Correct |
281 ms |
9084 KB |
Output is correct |
3 |
Correct |
222 ms |
8952 KB |
Output is correct |
4 |
Correct |
258 ms |
11088 KB |
Output is correct |
5 |
Correct |
268 ms |
8952 KB |
Output is correct |
6 |
Correct |
258 ms |
9208 KB |
Output is correct |
7 |
Correct |
259 ms |
9232 KB |
Output is correct |
8 |
Correct |
246 ms |
9080 KB |
Output is correct |
9 |
Correct |
274 ms |
9208 KB |
Output is correct |
10 |
Correct |
193 ms |
9336 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
290 ms |
11128 KB |
Output is correct |
20 |
Correct |
268 ms |
11000 KB |
Output is correct |
21 |
Correct |
277 ms |
11000 KB |
Output is correct |
22 |
Correct |
265 ms |
11256 KB |
Output is correct |
23 |
Correct |
263 ms |
9276 KB |
Output is correct |
24 |
Correct |
281 ms |
9084 KB |
Output is correct |
25 |
Correct |
222 ms |
8952 KB |
Output is correct |
26 |
Correct |
258 ms |
11088 KB |
Output is correct |
27 |
Correct |
268 ms |
8952 KB |
Output is correct |
28 |
Correct |
258 ms |
9208 KB |
Output is correct |
29 |
Correct |
259 ms |
9232 KB |
Output is correct |
30 |
Correct |
246 ms |
9080 KB |
Output is correct |
31 |
Correct |
274 ms |
9208 KB |
Output is correct |
32 |
Correct |
193 ms |
9336 KB |
Output is correct |
33 |
Correct |
0 ms |
384 KB |
Output is correct |
34 |
Correct |
0 ms |
384 KB |
Output is correct |
35 |
Correct |
0 ms |
384 KB |
Output is correct |
36 |
Correct |
0 ms |
384 KB |
Output is correct |
37 |
Correct |
273 ms |
11072 KB |
Output is correct |
38 |
Correct |
267 ms |
11232 KB |
Output is correct |
39 |
Correct |
268 ms |
11256 KB |
Output is correct |
40 |
Correct |
273 ms |
11256 KB |
Output is correct |
41 |
Correct |
264 ms |
11396 KB |
Output is correct |
42 |
Correct |
277 ms |
11256 KB |
Output is correct |
43 |
Correct |
268 ms |
11256 KB |
Output is correct |
44 |
Correct |
273 ms |
11128 KB |
Output is correct |
45 |
Correct |
267 ms |
11120 KB |
Output is correct |
46 |
Correct |
268 ms |
11128 KB |
Output is correct |
47 |
Correct |
254 ms |
11344 KB |
Output is correct |
48 |
Correct |
263 ms |
11128 KB |
Output is correct |
49 |
Correct |
268 ms |
11128 KB |
Output is correct |
50 |
Correct |
228 ms |
11000 KB |
Output is correct |
51 |
Correct |
261 ms |
11256 KB |
Output is correct |
52 |
Correct |
269 ms |
11000 KB |
Output is correct |
53 |
Correct |
264 ms |
11256 KB |
Output is correct |
54 |
Correct |
266 ms |
11292 KB |
Output is correct |
55 |
Correct |
248 ms |
11256 KB |
Output is correct |
56 |
Correct |
244 ms |
11516 KB |
Output is correct |
57 |
Correct |
192 ms |
11384 KB |
Output is correct |
58 |
Correct |
1 ms |
384 KB |
Output is correct |
59 |
Correct |
0 ms |
384 KB |
Output is correct |
60 |
Correct |
1 ms |
384 KB |
Output is correct |
61 |
Correct |
1 ms |
384 KB |
Output is correct |
62 |
Correct |
1 ms |
384 KB |
Output is correct |
63 |
Correct |
0 ms |
384 KB |
Output is correct |
64 |
Correct |
1 ms |
384 KB |
Output is correct |
65 |
Correct |
268 ms |
11000 KB |
Output is correct |
66 |
Correct |
263 ms |
11000 KB |
Output is correct |
67 |
Correct |
268 ms |
11000 KB |
Output is correct |
68 |
Correct |
269 ms |
11256 KB |
Output is correct |
69 |
Correct |
0 ms |
384 KB |
Output is correct |
70 |
Correct |
0 ms |
384 KB |
Output is correct |
71 |
Correct |
1 ms |
384 KB |
Output is correct |
72 |
Correct |
1 ms |
384 KB |
Output is correct |
73 |
Correct |
1 ms |
384 KB |
Output is correct |
74 |
Correct |
1 ms |
384 KB |
Output is correct |
75 |
Correct |
1 ms |
384 KB |
Output is correct |
76 |
Correct |
1 ms |
384 KB |
Output is correct |
77 |
Correct |
1 ms |
384 KB |
Output is correct |
78 |
Correct |
1 ms |
384 KB |
Output is correct |
79 |
Correct |
1 ms |
384 KB |
Output is correct |
80 |
Correct |
0 ms |
384 KB |
Output is correct |
81 |
Correct |
1 ms |
384 KB |
Output is correct |
82 |
Correct |
0 ms |
384 KB |
Output is correct |
83 |
Correct |
1 ms |
384 KB |
Output is correct |