#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
int id = 0;
map<string, int> mp;
vector<int> to(n * 2);
vector<int> deg(n * 2);
for (int i = 0; i < n; ++i) {
string s1, s2;
cin >> s1 >> s2;
if (!mp.count(s1)) {
mp[s1] = id++;
}
if (!mp.count(s2)) {
mp[s2] = id++;
}
if (s1 != s2) {
deg[mp[s2]]++;
}
to[mp[s1]] = mp[s2];
}
if (n % 2) {
cout << -1 << '\n';
return 0;
}
vector<bool> matched(id);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
for (int i = 0; i < id; ++i) {
if (to[i] != i && to[to[i]] == i) {
matched[i] = true;
}
pq.emplace(deg[i], i);
}
int ans = 0;
while (!pq.empty()) {
auto [d, v] = pq.top();
pq.pop();
if (matched[v]) {
continue;
}
ans++;
matched[v] = true;
if (!matched[to[v]]) {//I deleted his edge as it's not pointing to me
matched[to[v]] = true;
deg[to[to[v]]]--;
pq.emplace(deg[to[to[v]]], to[to[v]]);
}
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
165 ms |
11044 KB |
Output is correct |
5 |
Correct |
156 ms |
10820 KB |
Output is correct |
6 |
Correct |
157 ms |
10764 KB |
Output is correct |
7 |
Correct |
142 ms |
9740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
159 ms |
10856 KB |
Output is correct |
2 |
Correct |
159 ms |
10892 KB |
Output is correct |
3 |
Correct |
123 ms |
10764 KB |
Output is correct |
4 |
Correct |
146 ms |
9844 KB |
Output is correct |
5 |
Correct |
163 ms |
10888 KB |
Output is correct |
6 |
Correct |
163 ms |
10936 KB |
Output is correct |
7 |
Correct |
159 ms |
11024 KB |
Output is correct |
8 |
Correct |
144 ms |
10952 KB |
Output is correct |
9 |
Correct |
147 ms |
10808 KB |
Output is correct |
10 |
Correct |
113 ms |
11004 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
165 ms |
11044 KB |
Output is correct |
20 |
Correct |
156 ms |
10820 KB |
Output is correct |
21 |
Correct |
157 ms |
10764 KB |
Output is correct |
22 |
Correct |
142 ms |
9740 KB |
Output is correct |
23 |
Correct |
159 ms |
10856 KB |
Output is correct |
24 |
Correct |
159 ms |
10892 KB |
Output is correct |
25 |
Correct |
123 ms |
10764 KB |
Output is correct |
26 |
Correct |
146 ms |
9844 KB |
Output is correct |
27 |
Correct |
163 ms |
10888 KB |
Output is correct |
28 |
Correct |
163 ms |
10936 KB |
Output is correct |
29 |
Correct |
159 ms |
11024 KB |
Output is correct |
30 |
Correct |
144 ms |
10952 KB |
Output is correct |
31 |
Correct |
147 ms |
10808 KB |
Output is correct |
32 |
Correct |
113 ms |
11004 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
166 ms |
10876 KB |
Output is correct |
38 |
Correct |
161 ms |
10952 KB |
Output is correct |
39 |
Correct |
160 ms |
12940 KB |
Output is correct |
40 |
Correct |
159 ms |
13052 KB |
Output is correct |
41 |
Correct |
160 ms |
13028 KB |
Output is correct |
42 |
Correct |
159 ms |
13004 KB |
Output is correct |
43 |
Correct |
172 ms |
13368 KB |
Output is correct |
44 |
Correct |
160 ms |
13040 KB |
Output is correct |
45 |
Correct |
165 ms |
13004 KB |
Output is correct |
46 |
Correct |
164 ms |
13004 KB |
Output is correct |
47 |
Correct |
150 ms |
12928 KB |
Output is correct |
48 |
Correct |
161 ms |
13116 KB |
Output is correct |
49 |
Correct |
159 ms |
13064 KB |
Output is correct |
50 |
Correct |
122 ms |
13000 KB |
Output is correct |
51 |
Correct |
141 ms |
11948 KB |
Output is correct |
52 |
Correct |
161 ms |
13004 KB |
Output is correct |
53 |
Correct |
168 ms |
13144 KB |
Output is correct |
54 |
Correct |
158 ms |
13156 KB |
Output is correct |
55 |
Correct |
142 ms |
13004 KB |
Output is correct |
56 |
Correct |
147 ms |
13164 KB |
Output is correct |
57 |
Correct |
113 ms |
12940 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
1 ms |
600 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
156 ms |
13004 KB |
Output is correct |
66 |
Correct |
155 ms |
13004 KB |
Output is correct |
67 |
Correct |
157 ms |
12996 KB |
Output is correct |
68 |
Correct |
143 ms |
11860 KB |
Output is correct |
69 |
Correct |
1 ms |
348 KB |
Output is correct |
70 |
Correct |
0 ms |
348 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |
74 |
Correct |
0 ms |
344 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
0 ms |
348 KB |
Output is correct |
78 |
Correct |
0 ms |
344 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
0 ms |
348 KB |
Output is correct |
81 |
Correct |
0 ms |
348 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |