#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 100000;
map <string, int> u;
bool taken[N+5];
int f[N+5];
int deg[N+5];
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout.precision(10);
cout << fixed;
int n;
cin >> n;
int tjm = 0;
for(int i=1; i<=n; i++){
string _a, _b;
cin >> _a >> _b;
if(!u[_a]) u[_a] = ++tjm;
if(!u[_b]) u[_b] = ++tjm;
int a = u[_a], b = u[_b];
f[a] = b;
deg[b]++;
}
if(n%2){
cout << "-1\n";
return 0;
}
int cnt = 0;
queue <int> q;
for(int i=1; i<=n; i++){
if(f[f[i]] == i && f[i] != i) cnt++, taken[i] = 1;
else if(deg[i] == 0) q.push(i);
}
while(!q.empty()){
int v = q.front();
q.pop();
if(!taken[f[v]] && !taken[v] && v != f[v]){
taken[f[v]] = 1, taken[v] = 1, cnt++;
if(f[f[v]] != f[v]){
deg[f[f[v]]]--;
if(deg[f[f[v]]] == 0) q.push(f[f[v]]);
}
deg[v] = deg[f[v]] = -1;
}
else{
deg[f[v]]--;
if(deg[f[v]] == 0) q.push(f[v]);
}
}
for(int i=1; i<=n; i++){
if(!taken[i]){
int tr = 1;
taken[i] = 1;
int x = f[i];
while(x != i && !taken[x]){
tr++;
taken[x] = 1;
x = f[x];
}
cnt += tr/2;
}
}
cout << n - cnt << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
220 ms |
11048 KB |
Output is correct |
5 |
Correct |
245 ms |
11160 KB |
Output is correct |
6 |
Correct |
235 ms |
11124 KB |
Output is correct |
7 |
Correct |
245 ms |
10988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
232 ms |
11188 KB |
Output is correct |
2 |
Correct |
211 ms |
11192 KB |
Output is correct |
3 |
Correct |
193 ms |
11152 KB |
Output is correct |
4 |
Correct |
236 ms |
11076 KB |
Output is correct |
5 |
Correct |
212 ms |
11040 KB |
Output is correct |
6 |
Correct |
225 ms |
11300 KB |
Output is correct |
7 |
Correct |
216 ms |
11320 KB |
Output is correct |
8 |
Correct |
207 ms |
11316 KB |
Output is correct |
9 |
Correct |
205 ms |
11468 KB |
Output is correct |
10 |
Correct |
190 ms |
11176 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
220 ms |
11048 KB |
Output is correct |
20 |
Correct |
245 ms |
11160 KB |
Output is correct |
21 |
Correct |
235 ms |
11124 KB |
Output is correct |
22 |
Correct |
245 ms |
10988 KB |
Output is correct |
23 |
Correct |
232 ms |
11188 KB |
Output is correct |
24 |
Correct |
211 ms |
11192 KB |
Output is correct |
25 |
Correct |
193 ms |
11152 KB |
Output is correct |
26 |
Correct |
236 ms |
11076 KB |
Output is correct |
27 |
Correct |
212 ms |
11040 KB |
Output is correct |
28 |
Correct |
225 ms |
11300 KB |
Output is correct |
29 |
Correct |
216 ms |
11320 KB |
Output is correct |
30 |
Correct |
207 ms |
11316 KB |
Output is correct |
31 |
Correct |
205 ms |
11468 KB |
Output is correct |
32 |
Correct |
190 ms |
11176 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
262 ms |
11212 KB |
Output is correct |
38 |
Correct |
226 ms |
11316 KB |
Output is correct |
39 |
Correct |
234 ms |
11336 KB |
Output is correct |
40 |
Correct |
203 ms |
11272 KB |
Output is correct |
41 |
Correct |
209 ms |
11272 KB |
Output is correct |
42 |
Correct |
206 ms |
11268 KB |
Output is correct |
43 |
Correct |
206 ms |
11244 KB |
Output is correct |
44 |
Correct |
209 ms |
11388 KB |
Output is correct |
45 |
Correct |
209 ms |
11204 KB |
Output is correct |
46 |
Correct |
206 ms |
11296 KB |
Output is correct |
47 |
Correct |
222 ms |
11444 KB |
Output is correct |
48 |
Correct |
206 ms |
11280 KB |
Output is correct |
49 |
Correct |
208 ms |
11124 KB |
Output is correct |
50 |
Correct |
170 ms |
11152 KB |
Output is correct |
51 |
Correct |
222 ms |
11068 KB |
Output is correct |
52 |
Correct |
205 ms |
11160 KB |
Output is correct |
53 |
Correct |
240 ms |
11300 KB |
Output is correct |
54 |
Correct |
205 ms |
11272 KB |
Output is correct |
55 |
Correct |
228 ms |
11304 KB |
Output is correct |
56 |
Correct |
193 ms |
11420 KB |
Output is correct |
57 |
Correct |
160 ms |
11088 KB |
Output is correct |
58 |
Correct |
0 ms |
204 KB |
Output is correct |
59 |
Correct |
0 ms |
204 KB |
Output is correct |
60 |
Correct |
0 ms |
204 KB |
Output is correct |
61 |
Correct |
1 ms |
332 KB |
Output is correct |
62 |
Correct |
1 ms |
428 KB |
Output is correct |
63 |
Correct |
0 ms |
204 KB |
Output is correct |
64 |
Correct |
0 ms |
332 KB |
Output is correct |
65 |
Correct |
221 ms |
11156 KB |
Output is correct |
66 |
Correct |
223 ms |
11236 KB |
Output is correct |
67 |
Correct |
217 ms |
11096 KB |
Output is correct |
68 |
Correct |
214 ms |
10948 KB |
Output is correct |
69 |
Correct |
1 ms |
332 KB |
Output is correct |
70 |
Correct |
1 ms |
204 KB |
Output is correct |
71 |
Correct |
1 ms |
332 KB |
Output is correct |
72 |
Correct |
1 ms |
204 KB |
Output is correct |
73 |
Correct |
0 ms |
204 KB |
Output is correct |
74 |
Correct |
1 ms |
204 KB |
Output is correct |
75 |
Correct |
1 ms |
332 KB |
Output is correct |
76 |
Correct |
1 ms |
204 KB |
Output is correct |
77 |
Correct |
1 ms |
332 KB |
Output is correct |
78 |
Correct |
1 ms |
332 KB |
Output is correct |
79 |
Correct |
1 ms |
332 KB |
Output is correct |
80 |
Correct |
1 ms |
204 KB |
Output is correct |
81 |
Correct |
0 ms |
320 KB |
Output is correct |
82 |
Correct |
1 ms |
204 KB |
Output is correct |
83 |
Correct |
0 ms |
204 KB |
Output is correct |