#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define per(i, a, b) for (int i = b - 1; i >= a; --i)
#define trav(a, x) for (auto& a : x)
#define sz(a) a.size()
#define umap unordered_map
#define uset unordered_set
typedef pair<int, int> ii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
umap<string, string> out;
umap<string, int> in;
uset<string> unloved, vis;
int ans = 0, n;
void remove(string name) {
if (in.count(out[name]) && 1 == in[out[name]]--) unloved.insert(out[name]);
out.erase(name);
in.erase(name);
}
int si;
void dfs(string person) {
if (vis.count(person)) return;
vis.insert(person);
++si;
if(out.count(out[person]))
dfs(out[person]);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
if (n & 1) {
cout << -1 << endl;
return 0;
}
string n1, n2;
vector<string> arr(n * 2);
rep(i, 0, n) {
cin >> arr[i] >> arr[i + n];
out.emplace(arr[i], arr[i + n]);
in.emplace(arr[i], 0);
}
vector<string> rem;
rep(i, 0, n) {
n1 = arr[i];
n2 = arr[i + n];
if (n1 == n2) continue;
if (out[n2] == n1) rem.push_back(n1);
++in[n2];
}
trav(name, rem) {
out.erase(name);
in.erase(name);
}
trav(person, in) if (!person.second) unloved.insert(person.first);
while (!unloved.empty()) {
string name = *unloved.begin();
unloved.erase(unloved.begin());
if (!out.count(name) || !out.count(out[name])) continue;
++ans;
remove(out[name]);
out.erase(name);
in.erase(name);
}
trav(person,out){
si=0;
dfs(person.first);
ans+=(si+1)>>1;
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
309 ms |
42272 KB |
Output is correct |
5 |
Correct |
290 ms |
30512 KB |
Output is correct |
6 |
Correct |
308 ms |
43928 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
278 ms |
26128 KB |
Output is correct |
2 |
Correct |
278 ms |
25244 KB |
Output is correct |
3 |
Correct |
204 ms |
31396 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
240 ms |
24208 KB |
Output is correct |
6 |
Correct |
287 ms |
27824 KB |
Output is correct |
7 |
Correct |
287 ms |
27832 KB |
Output is correct |
8 |
Correct |
306 ms |
29480 KB |
Output is correct |
9 |
Correct |
285 ms |
29856 KB |
Output is correct |
10 |
Correct |
242 ms |
32512 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
309 ms |
42272 KB |
Output is correct |
20 |
Correct |
290 ms |
30512 KB |
Output is correct |
21 |
Correct |
308 ms |
43928 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
278 ms |
26128 KB |
Output is correct |
24 |
Correct |
278 ms |
25244 KB |
Output is correct |
25 |
Correct |
204 ms |
31396 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
240 ms |
24208 KB |
Output is correct |
28 |
Correct |
287 ms |
27824 KB |
Output is correct |
29 |
Correct |
287 ms |
27832 KB |
Output is correct |
30 |
Correct |
306 ms |
29480 KB |
Output is correct |
31 |
Correct |
285 ms |
29856 KB |
Output is correct |
32 |
Correct |
242 ms |
32512 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
208 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
269 ms |
27168 KB |
Output is correct |
38 |
Correct |
296 ms |
29708 KB |
Output is correct |
39 |
Correct |
275 ms |
29584 KB |
Output is correct |
40 |
Correct |
297 ms |
29964 KB |
Output is correct |
41 |
Correct |
281 ms |
29992 KB |
Output is correct |
42 |
Correct |
275 ms |
29172 KB |
Output is correct |
43 |
Correct |
281 ms |
29264 KB |
Output is correct |
44 |
Correct |
281 ms |
29068 KB |
Output is correct |
45 |
Correct |
290 ms |
29112 KB |
Output is correct |
46 |
Correct |
269 ms |
29068 KB |
Output is correct |
47 |
Correct |
288 ms |
31816 KB |
Output is correct |
48 |
Correct |
267 ms |
28148 KB |
Output is correct |
49 |
Correct |
270 ms |
27496 KB |
Output is correct |
50 |
Correct |
205 ms |
33548 KB |
Output is correct |
51 |
Correct |
1 ms |
332 KB |
Output is correct |
52 |
Correct |
219 ms |
26376 KB |
Output is correct |
53 |
Correct |
285 ms |
30032 KB |
Output is correct |
54 |
Correct |
286 ms |
30092 KB |
Output is correct |
55 |
Correct |
259 ms |
31628 KB |
Output is correct |
56 |
Correct |
290 ms |
31864 KB |
Output is correct |
57 |
Correct |
244 ms |
34544 KB |
Output is correct |
58 |
Correct |
1 ms |
204 KB |
Output is correct |
59 |
Correct |
1 ms |
204 KB |
Output is correct |
60 |
Correct |
1 ms |
204 KB |
Output is correct |
61 |
Correct |
1 ms |
204 KB |
Output is correct |
62 |
Correct |
1 ms |
204 KB |
Output is correct |
63 |
Correct |
1 ms |
204 KB |
Output is correct |
64 |
Correct |
1 ms |
204 KB |
Output is correct |
65 |
Correct |
297 ms |
44556 KB |
Output is correct |
66 |
Correct |
273 ms |
32396 KB |
Output is correct |
67 |
Correct |
299 ms |
46188 KB |
Output is correct |
68 |
Correct |
1 ms |
332 KB |
Output is correct |
69 |
Correct |
1 ms |
204 KB |
Output is correct |
70 |
Correct |
1 ms |
204 KB |
Output is correct |
71 |
Correct |
1 ms |
204 KB |
Output is correct |
72 |
Correct |
1 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 |
1 ms |
204 KB |
Output is correct |
77 |
Correct |
1 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 |
1 ms |
316 KB |
Output is correct |
83 |
Correct |
1 ms |
204 KB |
Output is correct |