#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
map<string,int> names;
int cur_name = 0;
vi graph(n);
vector<set<int>> rgraph(n);
for (int i = 0; i < n; i++) {
string a, b;
cin >> a >> b;
if (names.find(a) == names.end()) names[a] = cur_name++;
if (names.find(b) == names.end()) names[b] = cur_name++;
graph[names[a]] = names[b];
rgraph[names[b]].insert(names[a]);
}
if (n & 1) { cout << -1 << '\n'; return 0; }
int res = 0;
queue<int> q;
for (int i = 0; i < n; i++)
if (rgraph[i].size() == 0)
q.push(i);
set<int> in_cycles;
for (int i = 0; i < n; i++)
in_cycles.insert(i);
while (q.size()) {
res++;
int u = q.front(); q.pop();
int v = graph[u];
in_cycles.erase(u);
if (v == -1 || (graph[v] != -1 && graph[v] != v && graph[graph[v]] == v))
continue;
in_cycles.erase(v);
for (auto x : rgraph[v])
graph[x] = -1;
int w = graph[v];
if (w != -1) {
rgraph[w].erase(v);
if (rgraph[w].size() == 0)
q.push(w);
}
}
vector<bool> visited(n, false);
for (auto u : in_cycles) {
if (visited[u]) continue;
int v = u, len = 0;
while (!visited[v]) {
visited[v] = true;
v = graph[v];
len++;
}
if (len != 2)
res += (len + 1) / 2;
}
cout << res << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
247 ms |
22560 KB |
Output is correct |
5 |
Correct |
251 ms |
22620 KB |
Output is correct |
6 |
Correct |
248 ms |
22748 KB |
Output is correct |
7 |
Correct |
231 ms |
17868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
293 ms |
22672 KB |
Output is correct |
2 |
Correct |
293 ms |
22580 KB |
Output is correct |
3 |
Correct |
219 ms |
22508 KB |
Output is correct |
4 |
Correct |
228 ms |
17868 KB |
Output is correct |
5 |
Correct |
297 ms |
22616 KB |
Output is correct |
6 |
Correct |
286 ms |
22756 KB |
Output is correct |
7 |
Correct |
298 ms |
22772 KB |
Output is correct |
8 |
Correct |
258 ms |
22764 KB |
Output is correct |
9 |
Correct |
278 ms |
22932 KB |
Output is correct |
10 |
Correct |
215 ms |
22988 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
247 ms |
22560 KB |
Output is correct |
20 |
Correct |
251 ms |
22620 KB |
Output is correct |
21 |
Correct |
248 ms |
22748 KB |
Output is correct |
22 |
Correct |
231 ms |
17868 KB |
Output is correct |
23 |
Correct |
293 ms |
22672 KB |
Output is correct |
24 |
Correct |
293 ms |
22580 KB |
Output is correct |
25 |
Correct |
219 ms |
22508 KB |
Output is correct |
26 |
Correct |
228 ms |
17868 KB |
Output is correct |
27 |
Correct |
297 ms |
22616 KB |
Output is correct |
28 |
Correct |
286 ms |
22756 KB |
Output is correct |
29 |
Correct |
298 ms |
22772 KB |
Output is correct |
30 |
Correct |
258 ms |
22764 KB |
Output is correct |
31 |
Correct |
278 ms |
22932 KB |
Output is correct |
32 |
Correct |
215 ms |
22988 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 |
314 ms |
22680 KB |
Output is correct |
38 |
Correct |
289 ms |
22932 KB |
Output is correct |
39 |
Correct |
283 ms |
22860 KB |
Output is correct |
40 |
Correct |
298 ms |
22860 KB |
Output is correct |
41 |
Correct |
287 ms |
22976 KB |
Output is correct |
42 |
Correct |
288 ms |
22912 KB |
Output is correct |
43 |
Correct |
287 ms |
22984 KB |
Output is correct |
44 |
Correct |
288 ms |
22784 KB |
Output is correct |
45 |
Correct |
333 ms |
22860 KB |
Output is correct |
46 |
Correct |
289 ms |
22860 KB |
Output is correct |
47 |
Correct |
285 ms |
23020 KB |
Output is correct |
48 |
Correct |
304 ms |
22820 KB |
Output is correct |
49 |
Correct |
291 ms |
22832 KB |
Output is correct |
50 |
Correct |
229 ms |
22728 KB |
Output is correct |
51 |
Correct |
250 ms |
17964 KB |
Output is correct |
52 |
Correct |
300 ms |
22756 KB |
Output is correct |
53 |
Correct |
289 ms |
22860 KB |
Output is correct |
54 |
Correct |
287 ms |
22860 KB |
Output is correct |
55 |
Correct |
259 ms |
22860 KB |
Output is correct |
56 |
Correct |
283 ms |
23044 KB |
Output is correct |
57 |
Correct |
219 ms |
23080 KB |
Output is correct |
58 |
Correct |
1 ms |
212 KB |
Output is correct |
59 |
Correct |
0 ms |
212 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 |
212 KB |
Output is correct |
64 |
Correct |
1 ms |
212 KB |
Output is correct |
65 |
Correct |
260 ms |
22668 KB |
Output is correct |
66 |
Correct |
257 ms |
22656 KB |
Output is correct |
67 |
Correct |
256 ms |
22812 KB |
Output is correct |
68 |
Correct |
222 ms |
17996 KB |
Output is correct |
69 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
73 |
Correct |
0 ms |
212 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
0 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
0 ms |
212 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 |
212 KB |
Output is correct |
81 |
Correct |
1 ms |
212 KB |
Output is correct |
82 |
Correct |
0 ms |
212 KB |
Output is correct |
83 |
Correct |
1 ms |
212 KB |
Output is correct |