#include <bits/stdc++.h>
using namespace std;
int main(){
int n, ans = 0; cin >> n;
vector<pair<string,string>> in;
vector<int> belove(n), love(n);
map<string,int> M;
queue<int> nobody;
vector<bool> vis(n,false);
if(n & 1){
cout << "-1\n"; return 0;
}
for(int i = 0; i < n; ++i){
string s1, s2; cin >> s1 >> s2;
M[s1] = i;
in.push_back({s1,s2});
}
for(int i = 0; i < n; ++i){
love[M[in[i].first]] = M[in[i].second];
belove[M[in[i].second]]++;
}
for(int i = 0; i < n; ++i){
if(love[love[i]] == i && love[i] != i)vis[i] = true;
if(!belove[i]){nobody.push(i);}
}
while(!nobody.empty()){
auto x = nobody.front();
nobody.pop();
vis[x] = true;
if(!vis[love[x]]){
vis[love[x]] = true;
if(--belove[love[love[x]]] == 0)nobody.push(love[love[x]]);
}
ans++;
}
for(int i = 0; i < n; ++i){
if(!vis[i]){
vis[i] = true;
int last = i;
vis[last] = true;
int cnt = 1;
while(!vis[love[last]]){
cnt++;
last = love[last];
vis[last] = true;
}
ans+=(cnt+1)>>1;
}
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
376 ms |
17364 KB |
Output is correct |
5 |
Correct |
380 ms |
17440 KB |
Output is correct |
6 |
Correct |
384 ms |
17544 KB |
Output is correct |
7 |
Correct |
1 ms |
1152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
378 ms |
15448 KB |
Output is correct |
2 |
Correct |
381 ms |
15444 KB |
Output is correct |
3 |
Correct |
338 ms |
15444 KB |
Output is correct |
4 |
Correct |
1 ms |
1152 KB |
Output is correct |
5 |
Correct |
391 ms |
15320 KB |
Output is correct |
6 |
Correct |
370 ms |
15452 KB |
Output is correct |
7 |
Correct |
371 ms |
15480 KB |
Output is correct |
8 |
Correct |
358 ms |
15392 KB |
Output is correct |
9 |
Correct |
361 ms |
15604 KB |
Output is correct |
10 |
Correct |
309 ms |
15724 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
376 ms |
17364 KB |
Output is correct |
20 |
Correct |
380 ms |
17440 KB |
Output is correct |
21 |
Correct |
384 ms |
17544 KB |
Output is correct |
22 |
Correct |
1 ms |
1152 KB |
Output is correct |
23 |
Correct |
378 ms |
15448 KB |
Output is correct |
24 |
Correct |
381 ms |
15444 KB |
Output is correct |
25 |
Correct |
338 ms |
15444 KB |
Output is correct |
26 |
Correct |
1 ms |
1152 KB |
Output is correct |
27 |
Correct |
391 ms |
15320 KB |
Output is correct |
28 |
Correct |
370 ms |
15452 KB |
Output is correct |
29 |
Correct |
371 ms |
15480 KB |
Output is correct |
30 |
Correct |
358 ms |
15392 KB |
Output is correct |
31 |
Correct |
361 ms |
15604 KB |
Output is correct |
32 |
Correct |
309 ms |
15724 KB |
Output is correct |
33 |
Correct |
0 ms |
256 KB |
Output is correct |
34 |
Correct |
0 ms |
256 KB |
Output is correct |
35 |
Correct |
1 ms |
256 KB |
Output is correct |
36 |
Correct |
0 ms |
256 KB |
Output is correct |
37 |
Correct |
375 ms |
17460 KB |
Output is correct |
38 |
Correct |
388 ms |
17748 KB |
Output is correct |
39 |
Correct |
380 ms |
17748 KB |
Output is correct |
40 |
Correct |
383 ms |
17620 KB |
Output is correct |
41 |
Correct |
378 ms |
17552 KB |
Output is correct |
42 |
Correct |
376 ms |
17492 KB |
Output is correct |
43 |
Correct |
374 ms |
17492 KB |
Output is correct |
44 |
Correct |
382 ms |
17492 KB |
Output is correct |
45 |
Correct |
388 ms |
17620 KB |
Output is correct |
46 |
Correct |
377 ms |
17620 KB |
Output is correct |
47 |
Correct |
379 ms |
17620 KB |
Output is correct |
48 |
Correct |
380 ms |
17552 KB |
Output is correct |
49 |
Correct |
383 ms |
17512 KB |
Output is correct |
50 |
Correct |
330 ms |
17364 KB |
Output is correct |
51 |
Correct |
1 ms |
1152 KB |
Output is correct |
52 |
Correct |
416 ms |
17352 KB |
Output is correct |
53 |
Correct |
383 ms |
17748 KB |
Output is correct |
54 |
Correct |
394 ms |
17588 KB |
Output is correct |
55 |
Correct |
352 ms |
17672 KB |
Output is correct |
56 |
Correct |
363 ms |
17620 KB |
Output is correct |
57 |
Correct |
314 ms |
17880 KB |
Output is correct |
58 |
Correct |
1 ms |
256 KB |
Output is correct |
59 |
Correct |
0 ms |
256 KB |
Output is correct |
60 |
Correct |
0 ms |
256 KB |
Output is correct |
61 |
Correct |
1 ms |
256 KB |
Output is correct |
62 |
Correct |
0 ms |
256 KB |
Output is correct |
63 |
Correct |
1 ms |
256 KB |
Output is correct |
64 |
Correct |
1 ms |
256 KB |
Output is correct |
65 |
Correct |
385 ms |
17492 KB |
Output is correct |
66 |
Correct |
371 ms |
17364 KB |
Output is correct |
67 |
Correct |
384 ms |
17620 KB |
Output is correct |
68 |
Correct |
1 ms |
1152 KB |
Output is correct |
69 |
Correct |
0 ms |
256 KB |
Output is correct |
70 |
Correct |
1 ms |
256 KB |
Output is correct |
71 |
Correct |
1 ms |
256 KB |
Output is correct |
72 |
Correct |
1 ms |
256 KB |
Output is correct |
73 |
Correct |
1 ms |
256 KB |
Output is correct |
74 |
Correct |
1 ms |
256 KB |
Output is correct |
75 |
Correct |
1 ms |
256 KB |
Output is correct |
76 |
Correct |
1 ms |
256 KB |
Output is correct |
77 |
Correct |
0 ms |
256 KB |
Output is correct |
78 |
Correct |
0 ms |
256 KB |
Output is correct |
79 |
Correct |
1 ms |
256 KB |
Output is correct |
80 |
Correct |
1 ms |
256 KB |
Output is correct |
81 |
Correct |
1 ms |
384 KB |
Output is correct |
82 |
Correct |
0 ms |
256 KB |
Output is correct |
83 |
Correct |
1 ms |
256 KB |
Output is correct |