#include <bits/stdc++.h>
using namespace std;
map <string,pair<string,int> > love;
string s[100005];
int in[100005];
bool ok[100005];
int main(){
ios::sync_with_stdio(false);
int n;
cin >> n;
for (int i=1;i<=n;i++){
string t;
cin >> s[i] >> t;
love[s[i]]={t,i};
}
for (int i=1;i<=n;i++){
if (love[love[s[i]].first].first == s[i] && s[i] != love[s[i]].first) ok[i]=ok[love[love[s[i]].first].second]=true;
else in[love[love[s[i]].first].second]++;
}
int ans=0;
for (int i=1;i<=n;i++){
if (in[i] == 0 && !ok[i] && !ok[love[love[s[i]].first].second]){
ans++,ok[i]=ok[love[love[s[i]].first].second]=true;
string now=love[love[s[i]].first].first;
in[love[now].second]--;
while (in[love[now].second] == 0){
if (now != love[now].first && !ok[love[now].second] && !ok[love[love[now].first].second]){
ok[love[now].second]=ok[love[love[now].first].second]=true;
ans++;
now=love[love[now].first].first;
in[love[now].second]--;
}
else
break;
}
}
}
for (int i=1;i<=n;i++){
if (s[i] != love[s[i]].first && !ok[i] && !ok[love[love[s[i]].first].second]){
ans++,ok[i]=ok[love[love[s[i]].first].second]=true;
string now=love[love[s[i]].first].first;
in[love[now].second]--;
while (in[love[now].second] == 0){
if (now != love[now].first && !ok[love[now].second] && !ok[love[love[now].first].second]){
ok[love[now].second]=ok[love[love[now].first].second]=true;
ans++;
now=love[love[now].first].first;
in[love[now].second]--;
}
else
break;
}
}
}
for (int i=1;i<=n;i++){
if (!ok[i]) ans++;
}
if (n%2 == 1) cout << "-1\n";
else cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3464 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3460 KB |
Output is correct |
5 |
Correct |
2 ms |
3380 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
2 ms |
3412 KB |
Output is correct |
9 |
Correct |
2 ms |
3412 KB |
Output is correct |
10 |
Correct |
2 ms |
3412 KB |
Output is correct |
11 |
Correct |
2 ms |
3412 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
2 ms |
3412 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3464 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
428 ms |
17048 KB |
Output is correct |
5 |
Correct |
391 ms |
17100 KB |
Output is correct |
6 |
Correct |
414 ms |
17136 KB |
Output is correct |
7 |
Correct |
427 ms |
17036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
398 ms |
17044 KB |
Output is correct |
2 |
Correct |
387 ms |
16976 KB |
Output is correct |
3 |
Correct |
247 ms |
16924 KB |
Output is correct |
4 |
Correct |
394 ms |
17020 KB |
Output is correct |
5 |
Correct |
410 ms |
16972 KB |
Output is correct |
6 |
Correct |
409 ms |
17072 KB |
Output is correct |
7 |
Correct |
381 ms |
17100 KB |
Output is correct |
8 |
Correct |
342 ms |
17100 KB |
Output is correct |
9 |
Correct |
356 ms |
17160 KB |
Output is correct |
10 |
Correct |
325 ms |
16560 KB |
Output is correct |
11 |
Correct |
2 ms |
3412 KB |
Output is correct |
12 |
Correct |
2 ms |
3464 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
2 ms |
3412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3464 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3460 KB |
Output is correct |
5 |
Correct |
2 ms |
3380 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
2 ms |
3412 KB |
Output is correct |
9 |
Correct |
2 ms |
3412 KB |
Output is correct |
10 |
Correct |
2 ms |
3412 KB |
Output is correct |
11 |
Correct |
2 ms |
3412 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
2 ms |
3412 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
2 ms |
3412 KB |
Output is correct |
17 |
Correct |
2 ms |
3464 KB |
Output is correct |
18 |
Correct |
2 ms |
3412 KB |
Output is correct |
19 |
Correct |
428 ms |
17048 KB |
Output is correct |
20 |
Correct |
391 ms |
17100 KB |
Output is correct |
21 |
Correct |
414 ms |
17136 KB |
Output is correct |
22 |
Correct |
427 ms |
17036 KB |
Output is correct |
23 |
Correct |
398 ms |
17044 KB |
Output is correct |
24 |
Correct |
387 ms |
16976 KB |
Output is correct |
25 |
Correct |
247 ms |
16924 KB |
Output is correct |
26 |
Correct |
394 ms |
17020 KB |
Output is correct |
27 |
Correct |
410 ms |
16972 KB |
Output is correct |
28 |
Correct |
409 ms |
17072 KB |
Output is correct |
29 |
Correct |
381 ms |
17100 KB |
Output is correct |
30 |
Correct |
342 ms |
17100 KB |
Output is correct |
31 |
Correct |
356 ms |
17160 KB |
Output is correct |
32 |
Correct |
325 ms |
16560 KB |
Output is correct |
33 |
Correct |
2 ms |
3412 KB |
Output is correct |
34 |
Correct |
2 ms |
3464 KB |
Output is correct |
35 |
Correct |
2 ms |
3412 KB |
Output is correct |
36 |
Correct |
2 ms |
3412 KB |
Output is correct |
37 |
Correct |
449 ms |
17040 KB |
Output is correct |
38 |
Correct |
392 ms |
17072 KB |
Output is correct |
39 |
Correct |
398 ms |
17116 KB |
Output is correct |
40 |
Correct |
394 ms |
16984 KB |
Output is correct |
41 |
Correct |
403 ms |
17032 KB |
Output is correct |
42 |
Correct |
408 ms |
16928 KB |
Output is correct |
43 |
Correct |
390 ms |
17020 KB |
Output is correct |
44 |
Correct |
408 ms |
17036 KB |
Output is correct |
45 |
Correct |
379 ms |
17100 KB |
Output is correct |
46 |
Correct |
413 ms |
17140 KB |
Output is correct |
47 |
Correct |
371 ms |
17072 KB |
Output is correct |
48 |
Correct |
385 ms |
16976 KB |
Output is correct |
49 |
Correct |
412 ms |
17128 KB |
Output is correct |
50 |
Correct |
241 ms |
16896 KB |
Output is correct |
51 |
Correct |
381 ms |
17040 KB |
Output is correct |
52 |
Correct |
401 ms |
16968 KB |
Output is correct |
53 |
Correct |
407 ms |
17040 KB |
Output is correct |
54 |
Correct |
400 ms |
16944 KB |
Output is correct |
55 |
Correct |
352 ms |
16976 KB |
Output is correct |
56 |
Correct |
399 ms |
17112 KB |
Output is correct |
57 |
Correct |
318 ms |
16568 KB |
Output is correct |
58 |
Correct |
2 ms |
3412 KB |
Output is correct |
59 |
Correct |
2 ms |
3540 KB |
Output is correct |
60 |
Correct |
2 ms |
3412 KB |
Output is correct |
61 |
Correct |
2 ms |
3412 KB |
Output is correct |
62 |
Correct |
2 ms |
3412 KB |
Output is correct |
63 |
Correct |
2 ms |
3412 KB |
Output is correct |
64 |
Correct |
2 ms |
3412 KB |
Output is correct |
65 |
Correct |
376 ms |
17008 KB |
Output is correct |
66 |
Correct |
404 ms |
17036 KB |
Output is correct |
67 |
Correct |
408 ms |
17044 KB |
Output is correct |
68 |
Correct |
415 ms |
16972 KB |
Output is correct |
69 |
Correct |
2 ms |
3412 KB |
Output is correct |
70 |
Correct |
2 ms |
3412 KB |
Output is correct |
71 |
Correct |
2 ms |
3412 KB |
Output is correct |
72 |
Correct |
2 ms |
3412 KB |
Output is correct |
73 |
Correct |
2 ms |
3412 KB |
Output is correct |
74 |
Correct |
2 ms |
3412 KB |
Output is correct |
75 |
Correct |
2 ms |
3412 KB |
Output is correct |
76 |
Correct |
2 ms |
3412 KB |
Output is correct |
77 |
Correct |
2 ms |
3412 KB |
Output is correct |
78 |
Correct |
2 ms |
3412 KB |
Output is correct |
79 |
Correct |
2 ms |
3412 KB |
Output is correct |
80 |
Correct |
2 ms |
3412 KB |
Output is correct |
81 |
Correct |
2 ms |
3412 KB |
Output is correct |
82 |
Correct |
2 ms |
3464 KB |
Output is correct |
83 |
Correct |
2 ms |
3412 KB |
Output is correct |