#include <bits/stdc++.h>
using namespace std;
#define MAXN 100005
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define f first
#define s second
int n, ec[MAXN];
string st[MAXN], nx[MAXN];
map<string, int> idx;
set<int> adj[MAXN];
bool d[MAXN], v[MAXN];
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
FOR(i, 1, n) ec[i] = 0;
FOR(i, 1, n){
cin >> st[i];
idx.insert({st[i], i});
cin >> nx[i];
}
if(n % 2) {
cout << -1 << endl;
return 0;
}
int r = 0;
FOR(i, 1, n){
d[i] = (idx[nx[idx[nx[i]]]] == i && idx[nx[i]] != i);
}
FOR(i, 1, n){
if(i == idx[nx[i]] || d[i] || d[idx[nx[i]]]) continue;
ec[i]++, ec[idx[nx[i]]]++;
}
FOR(i, 1, n){
if(i == idx[nx[i]] || d[i] || d[idx[nx[i]]]) continue;
adj[i].insert(idx[nx[i]]);
//cout << "DCVVB: " << idx[nx[i]] << endl;
adj[idx[nx[i]]].insert(i);
}
set<pair<int, int>> se;
FOR(i, 1, n) if(!d[i]) se.insert({ec[i], i});
FOR(i, 1, n) v[i] = false;
while(se.size()){
auto si = *se.begin();
se.erase(se.begin());
v[si.s] = true;
if(si.f == 0){
r++;
//cout << st[si.s] << " FALLALEUJAH! " << endl;
}else{
int j = 0, jmn = INT_MAX;
for(int i : adj[si.s]){
if(jmn > ec[i]) j = i, jmn = ec[i];
}
r++;
//cout << st[si.s] << " ~ " << st[j] << " : " << si.s << " " << j << endl;
se.erase({jmn, j});
v[j] = true;
for(int j2 : adj[j]) if(!v[j2]) {
se.erase({ec[j2], j2});
adj[j2].erase(j), ec[j2]--;
se.insert({ec[j2], j2});
}
}
for(int i2 : adj[si.s]) if(!v[i2]) {
se.erase({ec[i2], i2});
adj[i2].erase(si.s), ec[i2]--;
se.insert({ec[i2], i2});
}
}
cout << r << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11356 KB |
Output is correct |
2 |
Correct |
2 ms |
11356 KB |
Output is correct |
3 |
Correct |
2 ms |
11356 KB |
Output is correct |
4 |
Correct |
2 ms |
11352 KB |
Output is correct |
5 |
Correct |
2 ms |
11356 KB |
Output is correct |
6 |
Correct |
2 ms |
11356 KB |
Output is correct |
7 |
Correct |
2 ms |
11356 KB |
Output is correct |
8 |
Correct |
2 ms |
11424 KB |
Output is correct |
9 |
Correct |
2 ms |
11356 KB |
Output is correct |
10 |
Correct |
3 ms |
11356 KB |
Output is correct |
11 |
Correct |
2 ms |
11356 KB |
Output is correct |
12 |
Correct |
2 ms |
11356 KB |
Output is correct |
13 |
Correct |
2 ms |
11356 KB |
Output is correct |
14 |
Correct |
2 ms |
11356 KB |
Output is correct |
15 |
Correct |
2 ms |
11356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11356 KB |
Output is correct |
2 |
Correct |
2 ms |
11356 KB |
Output is correct |
3 |
Correct |
2 ms |
11352 KB |
Output is correct |
4 |
Correct |
353 ms |
33988 KB |
Output is correct |
5 |
Correct |
336 ms |
32852 KB |
Output is correct |
6 |
Correct |
351 ms |
36024 KB |
Output is correct |
7 |
Correct |
65 ms |
21584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
425 ms |
33668 KB |
Output is correct |
2 |
Correct |
305 ms |
33704 KB |
Output is correct |
3 |
Correct |
156 ms |
24400 KB |
Output is correct |
4 |
Correct |
44 ms |
19392 KB |
Output is correct |
5 |
Correct |
292 ms |
33796 KB |
Output is correct |
6 |
Correct |
303 ms |
33876 KB |
Output is correct |
7 |
Correct |
308 ms |
33872 KB |
Output is correct |
8 |
Correct |
213 ms |
29008 KB |
Output is correct |
9 |
Correct |
278 ms |
33748 KB |
Output is correct |
10 |
Correct |
162 ms |
33796 KB |
Output is correct |
11 |
Correct |
2 ms |
11356 KB |
Output is correct |
12 |
Correct |
2 ms |
11356 KB |
Output is correct |
13 |
Correct |
2 ms |
11356 KB |
Output is correct |
14 |
Correct |
2 ms |
11356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11356 KB |
Output is correct |
2 |
Correct |
2 ms |
11356 KB |
Output is correct |
3 |
Correct |
2 ms |
11356 KB |
Output is correct |
4 |
Correct |
2 ms |
11352 KB |
Output is correct |
5 |
Correct |
2 ms |
11356 KB |
Output is correct |
6 |
Correct |
2 ms |
11356 KB |
Output is correct |
7 |
Correct |
2 ms |
11356 KB |
Output is correct |
8 |
Correct |
2 ms |
11424 KB |
Output is correct |
9 |
Correct |
2 ms |
11356 KB |
Output is correct |
10 |
Correct |
3 ms |
11356 KB |
Output is correct |
11 |
Correct |
2 ms |
11356 KB |
Output is correct |
12 |
Correct |
2 ms |
11356 KB |
Output is correct |
13 |
Correct |
2 ms |
11356 KB |
Output is correct |
14 |
Correct |
2 ms |
11356 KB |
Output is correct |
15 |
Correct |
2 ms |
11356 KB |
Output is correct |
16 |
Correct |
2 ms |
11356 KB |
Output is correct |
17 |
Correct |
2 ms |
11356 KB |
Output is correct |
18 |
Correct |
2 ms |
11352 KB |
Output is correct |
19 |
Correct |
353 ms |
33988 KB |
Output is correct |
20 |
Correct |
336 ms |
32852 KB |
Output is correct |
21 |
Correct |
351 ms |
36024 KB |
Output is correct |
22 |
Correct |
65 ms |
21584 KB |
Output is correct |
23 |
Correct |
425 ms |
33668 KB |
Output is correct |
24 |
Correct |
305 ms |
33704 KB |
Output is correct |
25 |
Correct |
156 ms |
24400 KB |
Output is correct |
26 |
Correct |
44 ms |
19392 KB |
Output is correct |
27 |
Correct |
292 ms |
33796 KB |
Output is correct |
28 |
Correct |
303 ms |
33876 KB |
Output is correct |
29 |
Correct |
308 ms |
33872 KB |
Output is correct |
30 |
Correct |
213 ms |
29008 KB |
Output is correct |
31 |
Correct |
278 ms |
33748 KB |
Output is correct |
32 |
Correct |
162 ms |
33796 KB |
Output is correct |
33 |
Correct |
2 ms |
11356 KB |
Output is correct |
34 |
Correct |
2 ms |
11356 KB |
Output is correct |
35 |
Correct |
2 ms |
11356 KB |
Output is correct |
36 |
Correct |
2 ms |
11356 KB |
Output is correct |
37 |
Correct |
346 ms |
35924 KB |
Output is correct |
38 |
Correct |
345 ms |
35924 KB |
Output is correct |
39 |
Correct |
323 ms |
35412 KB |
Output is correct |
40 |
Correct |
304 ms |
36176 KB |
Output is correct |
41 |
Correct |
310 ms |
35880 KB |
Output is correct |
42 |
Correct |
313 ms |
35956 KB |
Output is correct |
43 |
Correct |
332 ms |
35984 KB |
Output is correct |
44 |
Correct |
320 ms |
36060 KB |
Output is correct |
45 |
Correct |
315 ms |
35924 KB |
Output is correct |
46 |
Correct |
297 ms |
35840 KB |
Output is correct |
47 |
Correct |
297 ms |
35920 KB |
Output is correct |
48 |
Correct |
297 ms |
35788 KB |
Output is correct |
49 |
Correct |
308 ms |
35900 KB |
Output is correct |
50 |
Correct |
173 ms |
26560 KB |
Output is correct |
51 |
Correct |
50 ms |
21588 KB |
Output is correct |
52 |
Correct |
338 ms |
36176 KB |
Output is correct |
53 |
Correct |
352 ms |
35824 KB |
Output is correct |
54 |
Correct |
341 ms |
36176 KB |
Output is correct |
55 |
Correct |
253 ms |
31316 KB |
Output is correct |
56 |
Correct |
331 ms |
35916 KB |
Output is correct |
57 |
Correct |
175 ms |
35920 KB |
Output is correct |
58 |
Correct |
2 ms |
11352 KB |
Output is correct |
59 |
Correct |
2 ms |
11356 KB |
Output is correct |
60 |
Correct |
2 ms |
11356 KB |
Output is correct |
61 |
Correct |
2 ms |
11352 KB |
Output is correct |
62 |
Correct |
2 ms |
11356 KB |
Output is correct |
63 |
Correct |
2 ms |
11356 KB |
Output is correct |
64 |
Correct |
2 ms |
11356 KB |
Output is correct |
65 |
Correct |
351 ms |
35924 KB |
Output is correct |
66 |
Correct |
288 ms |
35156 KB |
Output is correct |
67 |
Correct |
327 ms |
35956 KB |
Output is correct |
68 |
Correct |
51 ms |
21588 KB |
Output is correct |
69 |
Correct |
2 ms |
11356 KB |
Output is correct |
70 |
Correct |
2 ms |
11356 KB |
Output is correct |
71 |
Correct |
2 ms |
11356 KB |
Output is correct |
72 |
Correct |
2 ms |
11356 KB |
Output is correct |
73 |
Correct |
2 ms |
11356 KB |
Output is correct |
74 |
Correct |
2 ms |
11356 KB |
Output is correct |
75 |
Correct |
2 ms |
11356 KB |
Output is correct |
76 |
Correct |
2 ms |
11356 KB |
Output is correct |
77 |
Correct |
2 ms |
11356 KB |
Output is correct |
78 |
Correct |
2 ms |
11356 KB |
Output is correct |
79 |
Correct |
2 ms |
11448 KB |
Output is correct |
80 |
Correct |
2 ms |
11356 KB |
Output is correct |
81 |
Correct |
2 ms |
11356 KB |
Output is correct |
82 |
Correct |
2 ms |
11448 KB |
Output is correct |
83 |
Correct |
2 ms |
11356 KB |
Output is correct |