#include <bits/stdc++.h>
using namespace std;
map<string, int> conv;
int id = 1;
struct node {
int edge;
set<int> redge; // loved by
};
node nodes[100005];
bool matched[100005];
bool seen[100005];
int ans = 0;
int ct = 0;
void dfs(int i) {
if (seen[i]) return;
seen[i] = true;
ct++;
dfs(nodes[i].edge);
}
int main() {
int n; cin >> n;
if (n % 2 == 1) {
cout << "-1"; return 0;
}
for (int i = 0; i < n; i++) {
string a, b; cin >> a >> b;
// a loves b
if (conv.find(a) == conv.end()) conv[a] = id++;
if (conv.find(b) == conv.end()) conv[b] = id++;
nodes[conv[a]].edge = conv[b];
nodes[conv[b]].redge.insert(conv[a]);
if (conv[a] != conv[b] && nodes[conv[b]].edge == conv[a]) {
matched[conv[a]] = matched[conv[b]] = true;
}
}
queue<int> leafs;
for (int i = 1; i <= n; i++) {
if (nodes[i].redge.size() == 0) leafs.push(i);
}
set<int> loners;
while (!leafs.empty()) {
auto top = leafs.front(); leafs.pop();
if (matched[nodes[top].edge]) {
// sad
loners.insert(top);
} else {
// match them
int to = nodes[top].edge;
int removenode = nodes[to].edge;
nodes[removenode].redge.erase(to);
if (nodes[removenode].redge.size() == 0) {
leafs.push(removenode);
}
ans++;
matched[to] = matched[top] = true;
}
}
vector<int> cycle;
for (int i = 1; i <= n; i++) {
if (matched[i]) seen[i] = true;
}
for (int i = 1; i <= n; i++) {
if (seen[i] || loners.find(i) != loners.end()) continue;
ct = 0;
dfs(i);
cycle.push_back(ct);
}
int lonersz = loners.size();
for (auto cur : cycle) {
if (cur == 1) lonersz++;
else if (cur == 2) continue;
else {
ans += (cur / 2);
if (cur % 2 == 1) lonersz++;
}
}
ans += (lonersz);
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5716 KB |
Output is correct |
2 |
Correct |
3 ms |
5780 KB |
Output is correct |
3 |
Correct |
3 ms |
5716 KB |
Output is correct |
4 |
Correct |
4 ms |
5716 KB |
Output is correct |
5 |
Correct |
3 ms |
5788 KB |
Output is correct |
6 |
Correct |
3 ms |
5716 KB |
Output is correct |
7 |
Correct |
4 ms |
5716 KB |
Output is correct |
8 |
Correct |
3 ms |
5716 KB |
Output is correct |
9 |
Correct |
4 ms |
5716 KB |
Output is correct |
10 |
Correct |
4 ms |
5844 KB |
Output is correct |
11 |
Correct |
4 ms |
5716 KB |
Output is correct |
12 |
Correct |
4 ms |
5716 KB |
Output is correct |
13 |
Correct |
3 ms |
5780 KB |
Output is correct |
14 |
Correct |
3 ms |
5716 KB |
Output is correct |
15 |
Correct |
3 ms |
5784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5696 KB |
Output is correct |
2 |
Correct |
3 ms |
5716 KB |
Output is correct |
3 |
Correct |
3 ms |
5716 KB |
Output is correct |
4 |
Correct |
406 ms |
18900 KB |
Output is correct |
5 |
Correct |
408 ms |
18888 KB |
Output is correct |
6 |
Correct |
417 ms |
18728 KB |
Output is correct |
7 |
Correct |
3 ms |
5716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
425 ms |
18784 KB |
Output is correct |
2 |
Correct |
426 ms |
18776 KB |
Output is correct |
3 |
Correct |
315 ms |
19248 KB |
Output is correct |
4 |
Correct |
3 ms |
5716 KB |
Output is correct |
5 |
Correct |
423 ms |
18704 KB |
Output is correct |
6 |
Correct |
420 ms |
18936 KB |
Output is correct |
7 |
Correct |
425 ms |
19020 KB |
Output is correct |
8 |
Correct |
359 ms |
18976 KB |
Output is correct |
9 |
Correct |
391 ms |
19840 KB |
Output is correct |
10 |
Correct |
354 ms |
23256 KB |
Output is correct |
11 |
Correct |
3 ms |
5716 KB |
Output is correct |
12 |
Correct |
3 ms |
5716 KB |
Output is correct |
13 |
Correct |
3 ms |
5784 KB |
Output is correct |
14 |
Correct |
3 ms |
5716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5716 KB |
Output is correct |
2 |
Correct |
3 ms |
5780 KB |
Output is correct |
3 |
Correct |
3 ms |
5716 KB |
Output is correct |
4 |
Correct |
4 ms |
5716 KB |
Output is correct |
5 |
Correct |
3 ms |
5788 KB |
Output is correct |
6 |
Correct |
3 ms |
5716 KB |
Output is correct |
7 |
Correct |
4 ms |
5716 KB |
Output is correct |
8 |
Correct |
3 ms |
5716 KB |
Output is correct |
9 |
Correct |
4 ms |
5716 KB |
Output is correct |
10 |
Correct |
4 ms |
5844 KB |
Output is correct |
11 |
Correct |
4 ms |
5716 KB |
Output is correct |
12 |
Correct |
4 ms |
5716 KB |
Output is correct |
13 |
Correct |
3 ms |
5780 KB |
Output is correct |
14 |
Correct |
3 ms |
5716 KB |
Output is correct |
15 |
Correct |
3 ms |
5784 KB |
Output is correct |
16 |
Correct |
5 ms |
5696 KB |
Output is correct |
17 |
Correct |
3 ms |
5716 KB |
Output is correct |
18 |
Correct |
3 ms |
5716 KB |
Output is correct |
19 |
Correct |
406 ms |
18900 KB |
Output is correct |
20 |
Correct |
408 ms |
18888 KB |
Output is correct |
21 |
Correct |
417 ms |
18728 KB |
Output is correct |
22 |
Correct |
3 ms |
5716 KB |
Output is correct |
23 |
Correct |
425 ms |
18784 KB |
Output is correct |
24 |
Correct |
426 ms |
18776 KB |
Output is correct |
25 |
Correct |
315 ms |
19248 KB |
Output is correct |
26 |
Correct |
3 ms |
5716 KB |
Output is correct |
27 |
Correct |
423 ms |
18704 KB |
Output is correct |
28 |
Correct |
420 ms |
18936 KB |
Output is correct |
29 |
Correct |
425 ms |
19020 KB |
Output is correct |
30 |
Correct |
359 ms |
18976 KB |
Output is correct |
31 |
Correct |
391 ms |
19840 KB |
Output is correct |
32 |
Correct |
354 ms |
23256 KB |
Output is correct |
33 |
Correct |
3 ms |
5716 KB |
Output is correct |
34 |
Correct |
3 ms |
5716 KB |
Output is correct |
35 |
Correct |
3 ms |
5784 KB |
Output is correct |
36 |
Correct |
3 ms |
5716 KB |
Output is correct |
37 |
Correct |
413 ms |
18724 KB |
Output is correct |
38 |
Correct |
472 ms |
18888 KB |
Output is correct |
39 |
Correct |
409 ms |
19124 KB |
Output is correct |
40 |
Correct |
442 ms |
19012 KB |
Output is correct |
41 |
Correct |
437 ms |
18932 KB |
Output is correct |
42 |
Correct |
515 ms |
18812 KB |
Output is correct |
43 |
Correct |
460 ms |
18884 KB |
Output is correct |
44 |
Correct |
500 ms |
18788 KB |
Output is correct |
45 |
Correct |
470 ms |
18808 KB |
Output is correct |
46 |
Correct |
482 ms |
18828 KB |
Output is correct |
47 |
Correct |
494 ms |
19852 KB |
Output is correct |
48 |
Correct |
427 ms |
18788 KB |
Output is correct |
49 |
Correct |
405 ms |
18800 KB |
Output is correct |
50 |
Correct |
300 ms |
19248 KB |
Output is correct |
51 |
Correct |
3 ms |
5716 KB |
Output is correct |
52 |
Correct |
410 ms |
18648 KB |
Output is correct |
53 |
Correct |
418 ms |
18916 KB |
Output is correct |
54 |
Correct |
413 ms |
18892 KB |
Output is correct |
55 |
Correct |
377 ms |
18908 KB |
Output is correct |
56 |
Correct |
428 ms |
19892 KB |
Output is correct |
57 |
Correct |
370 ms |
23220 KB |
Output is correct |
58 |
Correct |
3 ms |
5716 KB |
Output is correct |
59 |
Correct |
3 ms |
5716 KB |
Output is correct |
60 |
Correct |
3 ms |
5716 KB |
Output is correct |
61 |
Correct |
3 ms |
5780 KB |
Output is correct |
62 |
Correct |
3 ms |
5716 KB |
Output is correct |
63 |
Correct |
3 ms |
5784 KB |
Output is correct |
64 |
Correct |
3 ms |
5716 KB |
Output is correct |
65 |
Correct |
402 ms |
18656 KB |
Output is correct |
66 |
Correct |
463 ms |
18916 KB |
Output is correct |
67 |
Correct |
389 ms |
18632 KB |
Output is correct |
68 |
Correct |
3 ms |
5716 KB |
Output is correct |
69 |
Correct |
5 ms |
5780 KB |
Output is correct |
70 |
Correct |
3 ms |
5716 KB |
Output is correct |
71 |
Correct |
3 ms |
5780 KB |
Output is correct |
72 |
Correct |
3 ms |
5792 KB |
Output is correct |
73 |
Correct |
3 ms |
5716 KB |
Output is correct |
74 |
Correct |
3 ms |
5784 KB |
Output is correct |
75 |
Correct |
3 ms |
5784 KB |
Output is correct |
76 |
Correct |
3 ms |
5780 KB |
Output is correct |
77 |
Correct |
3 ms |
5716 KB |
Output is correct |
78 |
Correct |
3 ms |
5716 KB |
Output is correct |
79 |
Correct |
3 ms |
5784 KB |
Output is correct |
80 |
Correct |
3 ms |
5716 KB |
Output is correct |
81 |
Correct |
3 ms |
5716 KB |
Output is correct |
82 |
Correct |
3 ms |
5784 KB |
Output is correct |
83 |
Correct |
3 ms |
5716 KB |
Output is correct |