#include <bits/stdc++.h>
using namespace std;
const int MX = 2e5 + 10;
struct dsu{
int p[MX], cc[MX];
void init(){
for(int i = 0; i < MX; i++) p[i] = i, cc[i] = 1;
}
int f(int x){
if(p[x] == x) return x;
else return p[x] = f(p[x]);
}
void Join(int u, int v){
int fu = f(u), fv = f(v);
if(fu == fv) return;
p[fu] = fv;
cc[fv] += cc[fu];
}
} D;
int N, a[MX], b[MX], nx[MX], degree[MX], vis[MX], tim = 0;
map<string, int> mp;
bool dead[MX];
int dfs(int x){
if(vis[x]) return 0;
vis[x] = true;
return 1 + dfs(nx[x]);
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int ans = 0;
cin >> N;
for(int i = 0; i < N; i++){
string s, t; cin >> s >> t;
if(!mp.count(s)) mp[s] = ++tim;
if(!mp.count(t)) mp[t] = ++tim;
a[i] = mp[s], b[i] = mp[t];
nx[a[i]] = b[i];
degree[b[i]]++;
if(nx[b[i]] == a[i] && a[i] != b[i]){
vis[a[i]] = true;
vis[b[i]] = true;
}
}
if(N % 2 == 1){
cout << -1 << '\n';
return 0;
}
vector<int> qsolve;
for(int i = 1; i <= N; i++){
if(degree[i] == 0) qsolve.push_back(i);
}
for(; !qsolve.empty(); ){
int st = qsolve.back(); qsolve.pop_back();
if(vis[st] || vis[nx[st]]) continue;
ans++;
vis[st] = 1;
vis[nx[st]] = 1;
degree[nx[st]]--;
if(nx[nx[st]] == nx[st]) continue;
degree[nx[nx[st]]]--;
if(!vis[nx[nx[st]]] && degree[nx[nx[st]]] == 0) qsolve.push_back(nx[nx[st]]);
}
for(int i = 1; i <= N; i++){
if(vis[i]) continue;
int curr_sz = dfs(i);
ans += (curr_sz + 1) / 2;
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
336 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
147 ms |
12304 KB |
Output is correct |
5 |
Correct |
132 ms |
12200 KB |
Output is correct |
6 |
Correct |
141 ms |
12280 KB |
Output is correct |
7 |
Correct |
132 ms |
11848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
143 ms |
12488 KB |
Output is correct |
2 |
Correct |
137 ms |
12336 KB |
Output is correct |
3 |
Correct |
117 ms |
12232 KB |
Output is correct |
4 |
Correct |
129 ms |
11800 KB |
Output is correct |
5 |
Correct |
134 ms |
12264 KB |
Output is correct |
6 |
Correct |
130 ms |
12500 KB |
Output is correct |
7 |
Correct |
131 ms |
12528 KB |
Output is correct |
8 |
Correct |
125 ms |
12508 KB |
Output is correct |
9 |
Correct |
127 ms |
12640 KB |
Output is correct |
10 |
Correct |
116 ms |
12348 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
336 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
147 ms |
12304 KB |
Output is correct |
20 |
Correct |
132 ms |
12200 KB |
Output is correct |
21 |
Correct |
141 ms |
12280 KB |
Output is correct |
22 |
Correct |
132 ms |
11848 KB |
Output is correct |
23 |
Correct |
143 ms |
12488 KB |
Output is correct |
24 |
Correct |
137 ms |
12336 KB |
Output is correct |
25 |
Correct |
117 ms |
12232 KB |
Output is correct |
26 |
Correct |
129 ms |
11800 KB |
Output is correct |
27 |
Correct |
134 ms |
12264 KB |
Output is correct |
28 |
Correct |
130 ms |
12500 KB |
Output is correct |
29 |
Correct |
131 ms |
12528 KB |
Output is correct |
30 |
Correct |
125 ms |
12508 KB |
Output is correct |
31 |
Correct |
127 ms |
12640 KB |
Output is correct |
32 |
Correct |
116 ms |
12348 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
336 KB |
Output is correct |
37 |
Correct |
137 ms |
12368 KB |
Output is correct |
38 |
Correct |
131 ms |
12596 KB |
Output is correct |
39 |
Correct |
139 ms |
12616 KB |
Output is correct |
40 |
Correct |
135 ms |
12544 KB |
Output is correct |
41 |
Correct |
130 ms |
12556 KB |
Output is correct |
42 |
Correct |
132 ms |
12512 KB |
Output is correct |
43 |
Correct |
141 ms |
12472 KB |
Output is correct |
44 |
Correct |
131 ms |
12504 KB |
Output is correct |
45 |
Correct |
129 ms |
12508 KB |
Output is correct |
46 |
Correct |
134 ms |
12472 KB |
Output is correct |
47 |
Correct |
126 ms |
12600 KB |
Output is correct |
48 |
Correct |
138 ms |
12416 KB |
Output is correct |
49 |
Correct |
133 ms |
12352 KB |
Output is correct |
50 |
Correct |
117 ms |
12172 KB |
Output is correct |
51 |
Correct |
129 ms |
11852 KB |
Output is correct |
52 |
Correct |
130 ms |
12236 KB |
Output is correct |
53 |
Correct |
131 ms |
12528 KB |
Output is correct |
54 |
Correct |
135 ms |
12608 KB |
Output is correct |
55 |
Correct |
133 ms |
12588 KB |
Output is correct |
56 |
Correct |
126 ms |
12600 KB |
Output is correct |
57 |
Correct |
100 ms |
12372 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
0 ms |
340 KB |
Output is correct |
60 |
Correct |
0 ms |
340 KB |
Output is correct |
61 |
Correct |
0 ms |
340 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
0 ms |
340 KB |
Output is correct |
64 |
Correct |
0 ms |
340 KB |
Output is correct |
65 |
Correct |
138 ms |
12224 KB |
Output is correct |
66 |
Correct |
133 ms |
12212 KB |
Output is correct |
67 |
Correct |
135 ms |
12216 KB |
Output is correct |
68 |
Correct |
132 ms |
11784 KB |
Output is correct |
69 |
Correct |
0 ms |
340 KB |
Output is correct |
70 |
Correct |
1 ms |
332 KB |
Output is correct |
71 |
Correct |
0 ms |
340 KB |
Output is correct |
72 |
Correct |
1 ms |
340 KB |
Output is correct |
73 |
Correct |
0 ms |
340 KB |
Output is correct |
74 |
Correct |
1 ms |
352 KB |
Output is correct |
75 |
Correct |
0 ms |
340 KB |
Output is correct |
76 |
Correct |
1 ms |
340 KB |
Output is correct |
77 |
Correct |
0 ms |
340 KB |
Output is correct |
78 |
Correct |
1 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
340 KB |
Output is correct |
80 |
Correct |
0 ms |
340 KB |
Output is correct |
81 |
Correct |
1 ms |
336 KB |
Output is correct |
82 |
Correct |
0 ms |
340 KB |
Output is correct |
83 |
Correct |
0 ms |
340 KB |
Output is correct |