#include <bits/stdc++.h>
using namespace std ;
const int MAX = 1e5 + 10 ;
int arr[MAX] ;
int n ;
string s1 , s2 ;
map<string , int>mp ;
int id = 0 ;
int indeg[MAX] , to[MAX] ;
int match[MAX] ;
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n ;
for(int i = 1 ; i <= n ; ++i)
{
cin>>s1>>s2 ;
if(mp.find(s1) == mp.end())
mp[s1] = ++id ;
if(mp.find(s2) == mp.end())
mp[s2] = ++id ;
if(s2 != s1)
indeg[mp[s2]]++ ;
to[mp[s1]] = mp[s2] ;
}
if(n&1)
return cout<<-1<<"\n" , 0 ;
priority_queue< pair<int , int> , vector< pair<int , int> > , greater< pair<int , int> > >q ;
for(int i = 1 ; i <= n ; ++i)
{
q.push({indeg[i] , i}) ;
if(to[i] != i && to[to[i]] == i)
match[i] = 1 , match[to[i]] = 1 ;
}
int ans = 0 ;
while(!q.empty())
{
pair<int , int>p = q.top() ;
q.pop() ;
int node = p.second ;
if(match[node])
continue ;
ans++ ;
match[node] = 1 ;
int x = to[node] ;
if(match[x])
continue ;
match[x] = 1 ;
indeg[to[x]]-- ;
q.push({indeg[to[x]] , to[x]}) ;
}
return cout<<ans<<"\n" , 0 ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
336 KB |
Output is correct |
10 |
Correct |
0 ms |
336 KB |
Output is correct |
11 |
Correct |
0 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
0 ms |
336 KB |
Output is correct |
15 |
Correct |
0 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
207 ms |
12380 KB |
Output is correct |
5 |
Correct |
199 ms |
12480 KB |
Output is correct |
6 |
Correct |
251 ms |
12388 KB |
Output is correct |
7 |
Correct |
230 ms |
10952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
204 ms |
12352 KB |
Output is correct |
2 |
Correct |
252 ms |
12464 KB |
Output is correct |
3 |
Correct |
147 ms |
11960 KB |
Output is correct |
4 |
Correct |
176 ms |
10980 KB |
Output is correct |
5 |
Correct |
263 ms |
12312 KB |
Output is correct |
6 |
Correct |
202 ms |
12368 KB |
Output is correct |
7 |
Correct |
207 ms |
12332 KB |
Output is correct |
8 |
Correct |
205 ms |
12448 KB |
Output is correct |
9 |
Correct |
198 ms |
12384 KB |
Output is correct |
10 |
Correct |
151 ms |
11968 KB |
Output is correct |
11 |
Correct |
0 ms |
336 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
0 ms |
208 KB |
Output is correct |
14 |
Correct |
0 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
336 KB |
Output is correct |
10 |
Correct |
0 ms |
336 KB |
Output is correct |
11 |
Correct |
0 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
0 ms |
336 KB |
Output is correct |
15 |
Correct |
0 ms |
208 KB |
Output is correct |
16 |
Correct |
0 ms |
208 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
0 ms |
208 KB |
Output is correct |
19 |
Correct |
207 ms |
12380 KB |
Output is correct |
20 |
Correct |
199 ms |
12480 KB |
Output is correct |
21 |
Correct |
251 ms |
12388 KB |
Output is correct |
22 |
Correct |
230 ms |
10952 KB |
Output is correct |
23 |
Correct |
204 ms |
12352 KB |
Output is correct |
24 |
Correct |
252 ms |
12464 KB |
Output is correct |
25 |
Correct |
147 ms |
11960 KB |
Output is correct |
26 |
Correct |
176 ms |
10980 KB |
Output is correct |
27 |
Correct |
263 ms |
12312 KB |
Output is correct |
28 |
Correct |
202 ms |
12368 KB |
Output is correct |
29 |
Correct |
207 ms |
12332 KB |
Output is correct |
30 |
Correct |
205 ms |
12448 KB |
Output is correct |
31 |
Correct |
198 ms |
12384 KB |
Output is correct |
32 |
Correct |
151 ms |
11968 KB |
Output is correct |
33 |
Correct |
0 ms |
336 KB |
Output is correct |
34 |
Correct |
0 ms |
208 KB |
Output is correct |
35 |
Correct |
0 ms |
208 KB |
Output is correct |
36 |
Correct |
0 ms |
336 KB |
Output is correct |
37 |
Correct |
229 ms |
12280 KB |
Output is correct |
38 |
Correct |
273 ms |
12312 KB |
Output is correct |
39 |
Correct |
212 ms |
12604 KB |
Output is correct |
40 |
Correct |
215 ms |
12292 KB |
Output is correct |
41 |
Correct |
223 ms |
12384 KB |
Output is correct |
42 |
Correct |
200 ms |
12324 KB |
Output is correct |
43 |
Correct |
222 ms |
12332 KB |
Output is correct |
44 |
Correct |
217 ms |
12296 KB |
Output is correct |
45 |
Correct |
201 ms |
12356 KB |
Output is correct |
46 |
Correct |
241 ms |
12360 KB |
Output is correct |
47 |
Correct |
185 ms |
12336 KB |
Output is correct |
48 |
Correct |
246 ms |
12368 KB |
Output is correct |
49 |
Correct |
210 ms |
12336 KB |
Output is correct |
50 |
Correct |
161 ms |
11928 KB |
Output is correct |
51 |
Correct |
201 ms |
10976 KB |
Output is correct |
52 |
Correct |
200 ms |
12356 KB |
Output is correct |
53 |
Correct |
213 ms |
12368 KB |
Output is correct |
54 |
Correct |
212 ms |
12284 KB |
Output is correct |
55 |
Correct |
184 ms |
12376 KB |
Output is correct |
56 |
Correct |
226 ms |
12300 KB |
Output is correct |
57 |
Correct |
146 ms |
11952 KB |
Output is correct |
58 |
Correct |
0 ms |
336 KB |
Output is correct |
59 |
Correct |
0 ms |
208 KB |
Output is correct |
60 |
Correct |
0 ms |
336 KB |
Output is correct |
61 |
Correct |
0 ms |
332 KB |
Output is correct |
62 |
Correct |
0 ms |
208 KB |
Output is correct |
63 |
Correct |
0 ms |
336 KB |
Output is correct |
64 |
Correct |
0 ms |
336 KB |
Output is correct |
65 |
Correct |
256 ms |
12292 KB |
Output is correct |
66 |
Correct |
209 ms |
12396 KB |
Output is correct |
67 |
Correct |
203 ms |
12356 KB |
Output is correct |
68 |
Correct |
206 ms |
10956 KB |
Output is correct |
69 |
Correct |
0 ms |
328 KB |
Output is correct |
70 |
Correct |
0 ms |
336 KB |
Output is correct |
71 |
Correct |
0 ms |
336 KB |
Output is correct |
72 |
Correct |
0 ms |
336 KB |
Output is correct |
73 |
Correct |
0 ms |
208 KB |
Output is correct |
74 |
Correct |
0 ms |
336 KB |
Output is correct |
75 |
Correct |
0 ms |
336 KB |
Output is correct |
76 |
Correct |
0 ms |
336 KB |
Output is correct |
77 |
Correct |
0 ms |
336 KB |
Output is correct |
78 |
Correct |
1 ms |
336 KB |
Output is correct |
79 |
Correct |
1 ms |
336 KB |
Output is correct |
80 |
Correct |
0 ms |
208 KB |
Output is correct |
81 |
Correct |
0 ms |
208 KB |
Output is correct |
82 |
Correct |
0 ms |
336 KB |
Output is correct |
83 |
Correct |
0 ms |
336 KB |
Output is correct |