#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
int n;
vector<vector<int>> v;
vector<int> succ;
vector<bool> bl;
pair<int, int> cycle;
bool cycle_type;
void dfs_cycle(int nd, int ss){
if(bl[nd]){
cycle = {ss, nd};
return;
}
bl[nd] = 1;
unordered_map<int, int> mp;
for(int x: v[nd]) if(x != ss) dfs_cycle(x, nd), mp[x]++;
for(auto [x, c]: mp) if(c > 1){
cycle = {nd, x};
if(nd != x) cycle_type = 1;
}
}
int mc = 0;
bool dfs(int nd, int ss, bool type){
bool s = 0;
for(int x: v[nd])
if(x != ss && (nd != cycle.f || x != cycle.sc) && (nd != cycle.sc || x != cycle.f))
s|=dfs(x, nd, type);
if(ss == -1 && cycle_type) dfs(cycle.sc, nd, type);
if(type){
if(nd == cycle.f) return 0;
if(nd == cycle.sc){
mc++;
return 0;
}
}
if(s) mc++;
return !s;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
if(n%2 != 0){
cout << "-1\n";
exit(0);
}
v.assign(n, {});
succ.assign(n, -1);
bl.assign(n, 0);
unordered_map<str, int> mp;
int in = 0;
for(int i = 0; i < n; i++){
str a, b; cin >> a >> b;
if(mp.find(a) == mp.end()) mp[a] = in, in++;
if(mp.find(b) == mp.end()) mp[b] = in, in++;
succ[mp[a]] = mp[b];
v[mp[a]].pb(mp[b]);
v[mp[b]].pb(mp[a]);
}
int ans = 0;
for(int i = 0; i < n; i++){
if(bl[i]) continue;
int mx = 0;
cycle_type = 0;
dfs_cycle(i, -1);
mc = 0;
dfs(cycle.f, -1, 0);
mx = max(mx, mc);
if(cycle.f != cycle.sc){
mc = 0;
dfs(cycle.f, -1, 1);
mx = max(mx, mc+cycle_type);
}
ans+=mx;
}
ans = n-ans;
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
182 ms |
28088 KB |
Output is correct |
5 |
Correct |
167 ms |
15936 KB |
Output is correct |
6 |
Correct |
175 ms |
29996 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
149 ms |
15868 KB |
Output is correct |
2 |
Correct |
181 ms |
18512 KB |
Output is correct |
3 |
Correct |
97 ms |
15936 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
176 ms |
24088 KB |
Output is correct |
6 |
Correct |
186 ms |
16028 KB |
Output is correct |
7 |
Correct |
163 ms |
16084 KB |
Output is correct |
8 |
Correct |
117 ms |
15936 KB |
Output is correct |
9 |
Correct |
115 ms |
16532 KB |
Output is correct |
10 |
Correct |
83 ms |
21048 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
182 ms |
28088 KB |
Output is correct |
20 |
Correct |
167 ms |
15936 KB |
Output is correct |
21 |
Correct |
175 ms |
29996 KB |
Output is correct |
22 |
Correct |
1 ms |
328 KB |
Output is correct |
23 |
Correct |
149 ms |
15868 KB |
Output is correct |
24 |
Correct |
181 ms |
18512 KB |
Output is correct |
25 |
Correct |
97 ms |
15936 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
176 ms |
24088 KB |
Output is correct |
28 |
Correct |
186 ms |
16028 KB |
Output is correct |
29 |
Correct |
163 ms |
16084 KB |
Output is correct |
30 |
Correct |
117 ms |
15936 KB |
Output is correct |
31 |
Correct |
115 ms |
16532 KB |
Output is correct |
32 |
Correct |
83 ms |
21048 KB |
Output is correct |
33 |
Correct |
1 ms |
320 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
172 ms |
16128 KB |
Output is correct |
38 |
Correct |
161 ms |
19452 KB |
Output is correct |
39 |
Correct |
138 ms |
16004 KB |
Output is correct |
40 |
Correct |
167 ms |
16032 KB |
Output is correct |
41 |
Correct |
167 ms |
16000 KB |
Output is correct |
42 |
Correct |
141 ms |
16036 KB |
Output is correct |
43 |
Correct |
137 ms |
16104 KB |
Output is correct |
44 |
Correct |
153 ms |
16136 KB |
Output is correct |
45 |
Correct |
199 ms |
16132 KB |
Output is correct |
46 |
Correct |
153 ms |
16212 KB |
Output is correct |
47 |
Correct |
134 ms |
17080 KB |
Output is correct |
48 |
Correct |
173 ms |
16020 KB |
Output is correct |
49 |
Correct |
137 ms |
18636 KB |
Output is correct |
50 |
Correct |
81 ms |
15952 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
184 ms |
24028 KB |
Output is correct |
53 |
Correct |
150 ms |
15980 KB |
Output is correct |
54 |
Correct |
136 ms |
15964 KB |
Output is correct |
55 |
Correct |
133 ms |
15956 KB |
Output is correct |
56 |
Correct |
142 ms |
16564 KB |
Output is correct |
57 |
Correct |
79 ms |
21048 KB |
Output is correct |
58 |
Correct |
1 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
1 ms |
212 KB |
Output is correct |
61 |
Correct |
1 ms |
212 KB |
Output is correct |
62 |
Correct |
1 ms |
212 KB |
Output is correct |
63 |
Correct |
1 ms |
212 KB |
Output is correct |
64 |
Correct |
1 ms |
324 KB |
Output is correct |
65 |
Correct |
196 ms |
28200 KB |
Output is correct |
66 |
Correct |
125 ms |
15876 KB |
Output is correct |
67 |
Correct |
164 ms |
30036 KB |
Output is correct |
68 |
Correct |
1 ms |
340 KB |
Output is correct |
69 |
Correct |
1 ms |
212 KB |
Output is correct |
70 |
Correct |
1 ms |
212 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
1 ms |
324 KB |
Output is correct |
73 |
Correct |
1 ms |
212 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
1 ms |
212 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
212 KB |
Output is correct |
80 |
Correct |
1 ms |
212 KB |
Output is correct |
81 |
Correct |
1 ms |
212 KB |
Output is correct |
82 |
Correct |
1 ms |
212 KB |
Output is correct |
83 |
Correct |
1 ms |
212 KB |
Output is correct |