#include<bits/stdc++.h>
using namespace std;
constexpr int maxn = 1e5+10;
int deg[maxn], f[maxn], cnt;
bool mark[maxn];
map<string, int> mp;
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
memset(f, -1, sizeof f);
int n, ans = 0; cin >> n;
for(int i = 0; i < n; i++) {
string s, p; cin >> s >> p;
if(!mp.count(s)) mp[s] = ++cnt;
if(!mp.count(p)) mp[p] = ++cnt;
f[mp[s]] = mp[p];
++deg[mp[p]];
if(f[mp[p]] == mp[s] && s != p)
mark[mp[p]] = mark[mp[s]] = 1; // ja tao no par
}
queue<int> q;
for(int i = 1; i <= n; i++)
if(!deg[i]) q.push(i);
while(q.size()) {
int u = q.front(); q.pop();
if(mark[u] || mark[f[u]]) continue;
++ans;
mark[u] = 1;
mark[f[u]] = 1;
--deg[f[u]];
if(f[u] == f[f[u]]) continue;
if(!mark[f[f[u]]] && !(--deg[f[f[u]]]))
q.push(f[f[u]]);
}
for(int i = 1; i <= n; i++) {
if(mark[i]) continue;
mark[i] = 1;
int sz = 1, u = f[i];
while(!mark[u]) ++sz, mark[u] = 1, u = f[u];
ans += (sz+1)>>1;
}
printf("%d\n", n%2?-1:ans);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
748 KB |
Output is correct |
2 |
Correct |
1 ms |
748 KB |
Output is correct |
3 |
Correct |
1 ms |
748 KB |
Output is correct |
4 |
Correct |
1 ms |
748 KB |
Output is correct |
5 |
Correct |
1 ms |
748 KB |
Output is correct |
6 |
Correct |
1 ms |
748 KB |
Output is correct |
7 |
Correct |
1 ms |
748 KB |
Output is correct |
8 |
Correct |
1 ms |
748 KB |
Output is correct |
9 |
Correct |
1 ms |
748 KB |
Output is correct |
10 |
Correct |
1 ms |
748 KB |
Output is correct |
11 |
Correct |
1 ms |
748 KB |
Output is correct |
12 |
Correct |
1 ms |
748 KB |
Output is correct |
13 |
Correct |
0 ms |
748 KB |
Output is correct |
14 |
Correct |
1 ms |
748 KB |
Output is correct |
15 |
Correct |
1 ms |
748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
908 KB |
Output is correct |
2 |
Correct |
1 ms |
768 KB |
Output is correct |
3 |
Correct |
1 ms |
796 KB |
Output is correct |
4 |
Correct |
371 ms |
11396 KB |
Output is correct |
5 |
Correct |
369 ms |
11372 KB |
Output is correct |
6 |
Correct |
370 ms |
11292 KB |
Output is correct |
7 |
Correct |
369 ms |
11244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
355 ms |
11396 KB |
Output is correct |
2 |
Correct |
379 ms |
11416 KB |
Output is correct |
3 |
Correct |
342 ms |
11224 KB |
Output is correct |
4 |
Correct |
375 ms |
11372 KB |
Output is correct |
5 |
Correct |
347 ms |
11244 KB |
Output is correct |
6 |
Correct |
336 ms |
11372 KB |
Output is correct |
7 |
Correct |
339 ms |
11372 KB |
Output is correct |
8 |
Correct |
315 ms |
11500 KB |
Output is correct |
9 |
Correct |
342 ms |
11500 KB |
Output is correct |
10 |
Correct |
256 ms |
11372 KB |
Output is correct |
11 |
Correct |
1 ms |
748 KB |
Output is correct |
12 |
Correct |
1 ms |
748 KB |
Output is correct |
13 |
Correct |
1 ms |
748 KB |
Output is correct |
14 |
Correct |
1 ms |
748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
748 KB |
Output is correct |
2 |
Correct |
1 ms |
748 KB |
Output is correct |
3 |
Correct |
1 ms |
748 KB |
Output is correct |
4 |
Correct |
1 ms |
748 KB |
Output is correct |
5 |
Correct |
1 ms |
748 KB |
Output is correct |
6 |
Correct |
1 ms |
748 KB |
Output is correct |
7 |
Correct |
1 ms |
748 KB |
Output is correct |
8 |
Correct |
1 ms |
748 KB |
Output is correct |
9 |
Correct |
1 ms |
748 KB |
Output is correct |
10 |
Correct |
1 ms |
748 KB |
Output is correct |
11 |
Correct |
1 ms |
748 KB |
Output is correct |
12 |
Correct |
1 ms |
748 KB |
Output is correct |
13 |
Correct |
0 ms |
748 KB |
Output is correct |
14 |
Correct |
1 ms |
748 KB |
Output is correct |
15 |
Correct |
1 ms |
748 KB |
Output is correct |
16 |
Correct |
1 ms |
908 KB |
Output is correct |
17 |
Correct |
1 ms |
768 KB |
Output is correct |
18 |
Correct |
1 ms |
796 KB |
Output is correct |
19 |
Correct |
371 ms |
11396 KB |
Output is correct |
20 |
Correct |
369 ms |
11372 KB |
Output is correct |
21 |
Correct |
370 ms |
11292 KB |
Output is correct |
22 |
Correct |
369 ms |
11244 KB |
Output is correct |
23 |
Correct |
355 ms |
11396 KB |
Output is correct |
24 |
Correct |
379 ms |
11416 KB |
Output is correct |
25 |
Correct |
342 ms |
11224 KB |
Output is correct |
26 |
Correct |
375 ms |
11372 KB |
Output is correct |
27 |
Correct |
347 ms |
11244 KB |
Output is correct |
28 |
Correct |
336 ms |
11372 KB |
Output is correct |
29 |
Correct |
339 ms |
11372 KB |
Output is correct |
30 |
Correct |
315 ms |
11500 KB |
Output is correct |
31 |
Correct |
342 ms |
11500 KB |
Output is correct |
32 |
Correct |
256 ms |
11372 KB |
Output is correct |
33 |
Correct |
1 ms |
748 KB |
Output is correct |
34 |
Correct |
1 ms |
748 KB |
Output is correct |
35 |
Correct |
1 ms |
748 KB |
Output is correct |
36 |
Correct |
1 ms |
748 KB |
Output is correct |
37 |
Correct |
354 ms |
11244 KB |
Output is correct |
38 |
Correct |
348 ms |
11372 KB |
Output is correct |
39 |
Correct |
350 ms |
11372 KB |
Output is correct |
40 |
Correct |
333 ms |
11524 KB |
Output is correct |
41 |
Correct |
356 ms |
11456 KB |
Output is correct |
42 |
Correct |
340 ms |
11500 KB |
Output is correct |
43 |
Correct |
345 ms |
11372 KB |
Output is correct |
44 |
Correct |
358 ms |
11500 KB |
Output is correct |
45 |
Correct |
347 ms |
11500 KB |
Output is correct |
46 |
Correct |
350 ms |
11500 KB |
Output is correct |
47 |
Correct |
320 ms |
11500 KB |
Output is correct |
48 |
Correct |
358 ms |
11372 KB |
Output is correct |
49 |
Correct |
340 ms |
11244 KB |
Output is correct |
50 |
Correct |
285 ms |
11244 KB |
Output is correct |
51 |
Correct |
347 ms |
11500 KB |
Output is correct |
52 |
Correct |
353 ms |
11244 KB |
Output is correct |
53 |
Correct |
344 ms |
11516 KB |
Output is correct |
54 |
Correct |
347 ms |
11500 KB |
Output is correct |
55 |
Correct |
321 ms |
11372 KB |
Output is correct |
56 |
Correct |
320 ms |
11628 KB |
Output is correct |
57 |
Correct |
257 ms |
11372 KB |
Output is correct |
58 |
Correct |
1 ms |
748 KB |
Output is correct |
59 |
Correct |
1 ms |
748 KB |
Output is correct |
60 |
Correct |
1 ms |
748 KB |
Output is correct |
61 |
Correct |
1 ms |
748 KB |
Output is correct |
62 |
Correct |
1 ms |
748 KB |
Output is correct |
63 |
Correct |
1 ms |
748 KB |
Output is correct |
64 |
Correct |
1 ms |
748 KB |
Output is correct |
65 |
Correct |
351 ms |
11244 KB |
Output is correct |
66 |
Correct |
373 ms |
11432 KB |
Output is correct |
67 |
Correct |
342 ms |
11240 KB |
Output is correct |
68 |
Correct |
350 ms |
11372 KB |
Output is correct |
69 |
Correct |
1 ms |
748 KB |
Output is correct |
70 |
Correct |
1 ms |
748 KB |
Output is correct |
71 |
Correct |
1 ms |
748 KB |
Output is correct |
72 |
Correct |
1 ms |
748 KB |
Output is correct |
73 |
Correct |
1 ms |
748 KB |
Output is correct |
74 |
Correct |
1 ms |
748 KB |
Output is correct |
75 |
Correct |
1 ms |
748 KB |
Output is correct |
76 |
Correct |
1 ms |
748 KB |
Output is correct |
77 |
Correct |
1 ms |
748 KB |
Output is correct |
78 |
Correct |
1 ms |
748 KB |
Output is correct |
79 |
Correct |
1 ms |
748 KB |
Output is correct |
80 |
Correct |
1 ms |
748 KB |
Output is correct |
81 |
Correct |
1 ms |
748 KB |
Output is correct |
82 |
Correct |
1 ms |
748 KB |
Output is correct |
83 |
Correct |
1 ms |
748 KB |
Output is correct |