#include <bits/stdc++.h>
#define sz(x) (int)x.size()
#define vec vector
#define pb push_back
#define rall(x) x.rbegin(),x.rend()
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define f first
#define s second
using namespace std;
typedef unsigned long long ull;
auto rnd=bind(uniform_int_distribution<long long>(1,1e18),mt19937(time(0)));
int getans(int x){
if(x==2){
return 2;
}
return x/2;
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n;
cin>>n;
map<string,int>mp;
vec<string>obrr;
int cr=0;
vec<int>con(n,0);
vec<set<int>>obr(n,set<int>());
for(int i=0;i<n;i++){
string a,b;
cin>>a>>b;
if(!mp.count(a)) mp[a]=cr++,obrr.pb(a);
if(!mp.count(b)) mp[b]=cr++,obrr.pb(b);
con[mp[a]]=mp[b];
obr[mp[b]].insert(mp[a]);
}
if(n%2){
cout<<-1;
return 0;
}
assert(cr==n);
int ans=0;
queue<int>q;
vec<bool>inq(n,0);
for(int i=0;i<n;i++){
if(!sz(obr[i]) ) q.push(i),inq[i]=1;
}
vec<bool>used(n,0);
vec<int>is(n,0);/// did i used my edge i->u
/// #WARNING WITH CYCLES OF LENGTH 2
for(int i=0;i<n;i++){
if(i!=con[i] && con[con[i]]==i){
is[i]=1;
is[con[i]]=1;
used[i]=1;
}
}
while(!q.empty()){
int v=q.front();q.pop();
// cerr<<obrr[v]<<endl;
if(!used[v] && !used[con[v]]){
used[v]=1;
used[con[v]]=1;
int u=con[v];
obr[con[u]].erase(u);
is[v]=1;
if(!sz(obr[con[u]]) && !inq[con[u]]) inq[con[u]]=1,q.push(con[u]);
}
else if(!used[con[v]]){
obr[con[v]].erase(v);
if(!sz(obr[con[v]]) && !inq[con[v]]) inq[con[v]]=1,q.push(con[v]);
}
}
for(int i=0;i<n;i++){
if(used[i]) continue;
int v=i;
int cyc=0;
while(1){
cyc++;
if(cyc%2==0) is[v]=1;
used[v]=1;
v=con[v];
if(used[v]) break;
}
}
// for(int i=0;i<n;i++){
// cerr<<obrr[i]<<' '<<obrr[con[i]]<<' '<<is[i]<<endl;
// }
for(auto &z : is) ans+=1-z;
cout<<ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
248 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
361 ms |
23632 KB |
Output is correct |
5 |
Correct |
329 ms |
23596 KB |
Output is correct |
6 |
Correct |
348 ms |
23560 KB |
Output is correct |
7 |
Correct |
386 ms |
23112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
346 ms |
23652 KB |
Output is correct |
2 |
Correct |
348 ms |
23596 KB |
Output is correct |
3 |
Correct |
283 ms |
23612 KB |
Output is correct |
4 |
Correct |
344 ms |
23212 KB |
Output is correct |
5 |
Correct |
353 ms |
23572 KB |
Output is correct |
6 |
Correct |
347 ms |
23856 KB |
Output is correct |
7 |
Correct |
377 ms |
23724 KB |
Output is correct |
8 |
Correct |
328 ms |
23672 KB |
Output is correct |
9 |
Correct |
376 ms |
23892 KB |
Output is correct |
10 |
Correct |
339 ms |
24036 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
248 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 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 |
361 ms |
23632 KB |
Output is correct |
20 |
Correct |
329 ms |
23596 KB |
Output is correct |
21 |
Correct |
348 ms |
23560 KB |
Output is correct |
22 |
Correct |
386 ms |
23112 KB |
Output is correct |
23 |
Correct |
346 ms |
23652 KB |
Output is correct |
24 |
Correct |
348 ms |
23596 KB |
Output is correct |
25 |
Correct |
283 ms |
23612 KB |
Output is correct |
26 |
Correct |
344 ms |
23212 KB |
Output is correct |
27 |
Correct |
353 ms |
23572 KB |
Output is correct |
28 |
Correct |
347 ms |
23856 KB |
Output is correct |
29 |
Correct |
377 ms |
23724 KB |
Output is correct |
30 |
Correct |
328 ms |
23672 KB |
Output is correct |
31 |
Correct |
376 ms |
23892 KB |
Output is correct |
32 |
Correct |
339 ms |
24036 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
346 ms |
23668 KB |
Output is correct |
38 |
Correct |
359 ms |
23836 KB |
Output is correct |
39 |
Correct |
341 ms |
23808 KB |
Output is correct |
40 |
Correct |
347 ms |
23852 KB |
Output is correct |
41 |
Correct |
347 ms |
23768 KB |
Output is correct |
42 |
Correct |
401 ms |
23764 KB |
Output is correct |
43 |
Correct |
349 ms |
23680 KB |
Output is correct |
44 |
Correct |
342 ms |
23724 KB |
Output is correct |
45 |
Correct |
347 ms |
23776 KB |
Output is correct |
46 |
Correct |
348 ms |
23672 KB |
Output is correct |
47 |
Correct |
345 ms |
23888 KB |
Output is correct |
48 |
Correct |
395 ms |
23724 KB |
Output is correct |
49 |
Correct |
366 ms |
23604 KB |
Output is correct |
50 |
Correct |
290 ms |
23668 KB |
Output is correct |
51 |
Correct |
335 ms |
23160 KB |
Output is correct |
52 |
Correct |
359 ms |
23852 KB |
Output is correct |
53 |
Correct |
335 ms |
23776 KB |
Output is correct |
54 |
Correct |
341 ms |
23820 KB |
Output is correct |
55 |
Correct |
344 ms |
23776 KB |
Output is correct |
56 |
Correct |
340 ms |
23864 KB |
Output is correct |
57 |
Correct |
283 ms |
24140 KB |
Output is correct |
58 |
Correct |
0 ms |
204 KB |
Output is correct |
59 |
Correct |
0 ms |
204 KB |
Output is correct |
60 |
Correct |
1 ms |
320 KB |
Output is correct |
61 |
Correct |
1 ms |
324 KB |
Output is correct |
62 |
Correct |
1 ms |
320 KB |
Output is correct |
63 |
Correct |
1 ms |
204 KB |
Output is correct |
64 |
Correct |
1 ms |
204 KB |
Output is correct |
65 |
Correct |
329 ms |
23572 KB |
Output is correct |
66 |
Correct |
315 ms |
23592 KB |
Output is correct |
67 |
Correct |
338 ms |
23596 KB |
Output is correct |
68 |
Correct |
314 ms |
23200 KB |
Output is correct |
69 |
Correct |
0 ms |
204 KB |
Output is correct |
70 |
Correct |
1 ms |
204 KB |
Output is correct |
71 |
Correct |
1 ms |
316 KB |
Output is correct |
72 |
Correct |
0 ms |
204 KB |
Output is correct |
73 |
Correct |
1 ms |
204 KB |
Output is correct |
74 |
Correct |
1 ms |
204 KB |
Output is correct |
75 |
Correct |
1 ms |
204 KB |
Output is correct |
76 |
Correct |
0 ms |
332 KB |
Output is correct |
77 |
Correct |
0 ms |
204 KB |
Output is correct |
78 |
Correct |
1 ms |
204 KB |
Output is correct |
79 |
Correct |
1 ms |
204 KB |
Output is correct |
80 |
Correct |
1 ms |
204 KB |
Output is correct |
81 |
Correct |
1 ms |
204 KB |
Output is correct |
82 |
Correct |
0 ms |
204 KB |
Output is correct |
83 |
Correct |
1 ms |
204 KB |
Output is correct |