#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
gp_hash_table<string,int>ht;
vector<int>cnt[(int)1e5+5];
int out[(int)1e5+5],sz[(int)1e5+5];
bool vis[(int)1e5+5];
set<pair<int,int>>se;
int main(){
cin.tie(0)->sync_with_stdio(0);
int n,c=0,u,v,ans=0;
string s,t;
cin>>n;
if(n%2){
cout<<-1;
return 0;
}
for(int i=0;i<n;i++){
cin>>s>>t;
if(ht.find(s)!=ht.end())u=ht[s];
else u=ht[s]=c++;
if(ht.find(t)!=ht.end())v=ht[t];
else v=ht[t]=c++;
out[u]=v;
if(u==v)continue;
cnt[u].push_back(v),cnt[v].push_back(u);
sz[u]++,sz[v]++;
}
for(int i=0;i<n;i++){
if(out[out[i]]==i&&out[i]!=i&&!vis[i]){
ans+=2,vis[i]=vis[out[i]]=1;
for(int j:cnt[i])sz[j]--;
}
}
for(int i=0;i<n;i++)se.insert({sz[i],i});
while(se.size()){
auto p=*se.begin();
se.erase(se.begin());
if(p.first==0||vis[p.second])continue;
for(int i:cnt[p.second]){
if(!vis[i]){
vis[p.second]=vis[i]=1;
for(int j:cnt[p.second]){
if(!vis[j]){
se.erase({sz[j],j});
sz[j]--;
se.insert({sz[j],j});
}
}
for(int j:cnt[i]){
if(!vis[j]){
se.erase({sz[j],j});
sz[j]--;
se.insert({sz[j],j});
}
}
ans++;
break;
}
}
}
cout<<n-ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3420 KB |
Output is correct |
2 |
Correct |
1 ms |
3420 KB |
Output is correct |
3 |
Correct |
1 ms |
3420 KB |
Output is correct |
4 |
Correct |
2 ms |
3524 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
1 ms |
3420 KB |
Output is correct |
7 |
Correct |
1 ms |
3420 KB |
Output is correct |
8 |
Correct |
1 ms |
3420 KB |
Output is correct |
9 |
Correct |
1 ms |
3420 KB |
Output is correct |
10 |
Correct |
2 ms |
3524 KB |
Output is correct |
11 |
Correct |
1 ms |
3540 KB |
Output is correct |
12 |
Correct |
1 ms |
3500 KB |
Output is correct |
13 |
Correct |
1 ms |
3420 KB |
Output is correct |
14 |
Correct |
1 ms |
3528 KB |
Output is correct |
15 |
Correct |
1 ms |
3508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3416 KB |
Output is correct |
2 |
Correct |
1 ms |
3420 KB |
Output is correct |
3 |
Correct |
1 ms |
3528 KB |
Output is correct |
4 |
Correct |
122 ms |
31600 KB |
Output is correct |
5 |
Correct |
111 ms |
31848 KB |
Output is correct |
6 |
Correct |
115 ms |
31948 KB |
Output is correct |
7 |
Correct |
2 ms |
3420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
106 ms |
29860 KB |
Output is correct |
2 |
Correct |
109 ms |
31868 KB |
Output is correct |
3 |
Correct |
61 ms |
24136 KB |
Output is correct |
4 |
Correct |
1 ms |
3420 KB |
Output is correct |
5 |
Correct |
106 ms |
30788 KB |
Output is correct |
6 |
Correct |
137 ms |
31976 KB |
Output is correct |
7 |
Correct |
115 ms |
31948 KB |
Output is correct |
8 |
Correct |
82 ms |
30916 KB |
Output is correct |
9 |
Correct |
104 ms |
32124 KB |
Output is correct |
10 |
Correct |
82 ms |
27536 KB |
Output is correct |
11 |
Correct |
1 ms |
3420 KB |
Output is correct |
12 |
Correct |
1 ms |
3420 KB |
Output is correct |
13 |
Correct |
1 ms |
3420 KB |
Output is correct |
14 |
Correct |
2 ms |
3420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3420 KB |
Output is correct |
2 |
Correct |
1 ms |
3420 KB |
Output is correct |
3 |
Correct |
1 ms |
3420 KB |
Output is correct |
4 |
Correct |
2 ms |
3524 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
1 ms |
3420 KB |
Output is correct |
7 |
Correct |
1 ms |
3420 KB |
Output is correct |
8 |
Correct |
1 ms |
3420 KB |
Output is correct |
9 |
Correct |
1 ms |
3420 KB |
Output is correct |
10 |
Correct |
2 ms |
3524 KB |
Output is correct |
11 |
Correct |
1 ms |
3540 KB |
Output is correct |
12 |
Correct |
1 ms |
3500 KB |
Output is correct |
13 |
Correct |
1 ms |
3420 KB |
Output is correct |
14 |
Correct |
1 ms |
3528 KB |
Output is correct |
15 |
Correct |
1 ms |
3508 KB |
Output is correct |
16 |
Correct |
1 ms |
3416 KB |
Output is correct |
17 |
Correct |
1 ms |
3420 KB |
Output is correct |
18 |
Correct |
1 ms |
3528 KB |
Output is correct |
19 |
Correct |
122 ms |
31600 KB |
Output is correct |
20 |
Correct |
111 ms |
31848 KB |
Output is correct |
21 |
Correct |
115 ms |
31948 KB |
Output is correct |
22 |
Correct |
2 ms |
3420 KB |
Output is correct |
23 |
Correct |
106 ms |
29860 KB |
Output is correct |
24 |
Correct |
109 ms |
31868 KB |
Output is correct |
25 |
Correct |
61 ms |
24136 KB |
Output is correct |
26 |
Correct |
1 ms |
3420 KB |
Output is correct |
27 |
Correct |
106 ms |
30788 KB |
Output is correct |
28 |
Correct |
137 ms |
31976 KB |
Output is correct |
29 |
Correct |
115 ms |
31948 KB |
Output is correct |
30 |
Correct |
82 ms |
30916 KB |
Output is correct |
31 |
Correct |
104 ms |
32124 KB |
Output is correct |
32 |
Correct |
82 ms |
27536 KB |
Output is correct |
33 |
Correct |
1 ms |
3420 KB |
Output is correct |
34 |
Correct |
1 ms |
3420 KB |
Output is correct |
35 |
Correct |
1 ms |
3420 KB |
Output is correct |
36 |
Correct |
2 ms |
3420 KB |
Output is correct |
37 |
Correct |
107 ms |
32040 KB |
Output is correct |
38 |
Correct |
136 ms |
32048 KB |
Output is correct |
39 |
Correct |
133 ms |
29904 KB |
Output is correct |
40 |
Correct |
108 ms |
32200 KB |
Output is correct |
41 |
Correct |
131 ms |
32240 KB |
Output is correct |
42 |
Correct |
111 ms |
31900 KB |
Output is correct |
43 |
Correct |
115 ms |
32096 KB |
Output is correct |
44 |
Correct |
118 ms |
31952 KB |
Output is correct |
45 |
Correct |
140 ms |
32136 KB |
Output is correct |
46 |
Correct |
112 ms |
31948 KB |
Output is correct |
47 |
Correct |
120 ms |
32052 KB |
Output is correct |
48 |
Correct |
106 ms |
31988 KB |
Output is correct |
49 |
Correct |
119 ms |
31948 KB |
Output is correct |
50 |
Correct |
56 ms |
23976 KB |
Output is correct |
51 |
Correct |
2 ms |
3416 KB |
Output is correct |
52 |
Correct |
101 ms |
30848 KB |
Output is correct |
53 |
Correct |
109 ms |
32044 KB |
Output is correct |
54 |
Correct |
109 ms |
31952 KB |
Output is correct |
55 |
Correct |
80 ms |
31088 KB |
Output is correct |
56 |
Correct |
106 ms |
31952 KB |
Output is correct |
57 |
Correct |
81 ms |
27692 KB |
Output is correct |
58 |
Correct |
1 ms |
3416 KB |
Output is correct |
59 |
Correct |
1 ms |
3420 KB |
Output is correct |
60 |
Correct |
1 ms |
3420 KB |
Output is correct |
61 |
Correct |
1 ms |
3416 KB |
Output is correct |
62 |
Correct |
1 ms |
3416 KB |
Output is correct |
63 |
Correct |
1 ms |
3420 KB |
Output is correct |
64 |
Correct |
1 ms |
3420 KB |
Output is correct |
65 |
Correct |
107 ms |
31660 KB |
Output is correct |
66 |
Correct |
98 ms |
31944 KB |
Output is correct |
67 |
Correct |
109 ms |
31944 KB |
Output is correct |
68 |
Correct |
1 ms |
3416 KB |
Output is correct |
69 |
Correct |
1 ms |
3416 KB |
Output is correct |
70 |
Correct |
1 ms |
3420 KB |
Output is correct |
71 |
Correct |
1 ms |
3420 KB |
Output is correct |
72 |
Correct |
1 ms |
3420 KB |
Output is correct |
73 |
Correct |
1 ms |
3420 KB |
Output is correct |
74 |
Correct |
1 ms |
3420 KB |
Output is correct |
75 |
Correct |
1 ms |
3416 KB |
Output is correct |
76 |
Correct |
1 ms |
3420 KB |
Output is correct |
77 |
Correct |
1 ms |
3420 KB |
Output is correct |
78 |
Correct |
1 ms |
3420 KB |
Output is correct |
79 |
Correct |
1 ms |
3420 KB |
Output is correct |
80 |
Correct |
2 ms |
3420 KB |
Output is correct |
81 |
Correct |
1 ms |
3420 KB |
Output is correct |
82 |
Correct |
1 ms |
3420 KB |
Output is correct |
83 |
Correct |
1 ms |
3420 KB |
Output is correct |