#include <algorithm>
#include <array>
#include <iostream>
#include <vector>
using namespace std;
#define ar array
const int N = 1e5;
vector<int> g[N], cc;
bool on[N];
int o[N], v[N];
void dfs(int i) {
int j = o[i];
if (v[j] == 0) {
v[j] = 1;
dfs(j);
} else if (v[j] == 1)
cc.push_back(j);
v[i] = 2;
}
ar<int, 2> dp(int i) {
int t01 = 0, tm = 0;
for (int j : g[i])
if (!on[j]) {
ar<int, 2> a = dp(j);
t01 += max(a[0], a[1]), tm = max(tm, 1 + a[0] - max(a[0], a[1]));
}
return {t01, t01 + tm};
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
static ar<string, 2> e[N];
static string c[N];
for (int i = 0; i < n; i++) cin >> e[i][0] >> e[i][1], c[i] = e[i][0];
if (n % 2) {
cout << -1 << '\n';
return 0;
}
sort(c, c + n);
for (int h = 0; h < n; h++) {
int u = lower_bound(c, c + n, e[h][0]) - c;
int v = lower_bound(c, c + n, e[h][1]) - c;
o[u] = v;
g[v].push_back(u);
}
for (int i = 0; i < n; i++)
if (!v[i]) {
v[i] = 1;
dfs(i);
}
int p0 = 0, p1 = 0;
for (int i : cc) {
int u = i;
vector<ar<int, 2>> f;
do {
on[u] = 1;
u = o[u];
} while (u != i);
u = i;
do {
f.push_back(dp(u));
u = o[u];
} while (u != i);
if (f.size() == 1)
p1 += max(f[0][0], f[0][1]);
else if (f.size() == 2)
p0 += 1, p1 += f[0][0] + f[1][0];
else {
int x = 0;
for (int it = 0; it < 2; it++) {
ar<int, 2> t = f[0];
for (int i = 1; i < (int)f.size(); i++)
t = {max(t[0], t[1]) + f[i][0],
max(t[0] + f[i][0] + 1, max(t[0], t[1]) + f[i][1])};
x = max(x, max(t[0], t[1]));
rotate(f.begin(), f.begin() + 1, f.end());
}
p1 += x;
}
}
int p2 = n / 2 - p0 - p1;
cout << p1 + p2 * 2 << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12892 KB |
Output is correct |
2 |
Correct |
3 ms |
12892 KB |
Output is correct |
3 |
Correct |
4 ms |
12892 KB |
Output is correct |
4 |
Correct |
3 ms |
12888 KB |
Output is correct |
5 |
Correct |
3 ms |
12892 KB |
Output is correct |
6 |
Correct |
4 ms |
12892 KB |
Output is correct |
7 |
Correct |
3 ms |
12892 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
3 ms |
12892 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
11 |
Correct |
3 ms |
12888 KB |
Output is correct |
12 |
Correct |
3 ms |
12892 KB |
Output is correct |
13 |
Correct |
4 ms |
12892 KB |
Output is correct |
14 |
Correct |
3 ms |
12892 KB |
Output is correct |
15 |
Correct |
3 ms |
12760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12892 KB |
Output is correct |
2 |
Correct |
4 ms |
12892 KB |
Output is correct |
3 |
Correct |
3 ms |
12764 KB |
Output is correct |
4 |
Correct |
98 ms |
23492 KB |
Output is correct |
5 |
Correct |
90 ms |
18508 KB |
Output is correct |
6 |
Correct |
106 ms |
24012 KB |
Output is correct |
7 |
Correct |
18 ms |
14928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
17480 KB |
Output is correct |
2 |
Correct |
110 ms |
19012 KB |
Output is correct |
3 |
Correct |
97 ms |
18888 KB |
Output is correct |
4 |
Correct |
15 ms |
14940 KB |
Output is correct |
5 |
Correct |
92 ms |
22464 KB |
Output is correct |
6 |
Correct |
87 ms |
16636 KB |
Output is correct |
7 |
Correct |
84 ms |
16736 KB |
Output is correct |
8 |
Correct |
80 ms |
17456 KB |
Output is correct |
9 |
Correct |
81 ms |
16272 KB |
Output is correct |
10 |
Correct |
61 ms |
15716 KB |
Output is correct |
11 |
Correct |
4 ms |
12892 KB |
Output is correct |
12 |
Correct |
3 ms |
13144 KB |
Output is correct |
13 |
Correct |
4 ms |
12892 KB |
Output is correct |
14 |
Correct |
4 ms |
12764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12892 KB |
Output is correct |
2 |
Correct |
3 ms |
12892 KB |
Output is correct |
3 |
Correct |
4 ms |
12892 KB |
Output is correct |
4 |
Correct |
3 ms |
12888 KB |
Output is correct |
5 |
Correct |
3 ms |
12892 KB |
Output is correct |
6 |
Correct |
4 ms |
12892 KB |
Output is correct |
7 |
Correct |
3 ms |
12892 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
3 ms |
12892 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
11 |
Correct |
3 ms |
12888 KB |
Output is correct |
12 |
Correct |
3 ms |
12892 KB |
Output is correct |
13 |
Correct |
4 ms |
12892 KB |
Output is correct |
14 |
Correct |
3 ms |
12892 KB |
Output is correct |
15 |
Correct |
3 ms |
12760 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
4 ms |
12892 KB |
Output is correct |
18 |
Correct |
3 ms |
12764 KB |
Output is correct |
19 |
Correct |
98 ms |
23492 KB |
Output is correct |
20 |
Correct |
90 ms |
18508 KB |
Output is correct |
21 |
Correct |
106 ms |
24012 KB |
Output is correct |
22 |
Correct |
18 ms |
14928 KB |
Output is correct |
23 |
Correct |
93 ms |
17480 KB |
Output is correct |
24 |
Correct |
110 ms |
19012 KB |
Output is correct |
25 |
Correct |
97 ms |
18888 KB |
Output is correct |
26 |
Correct |
15 ms |
14940 KB |
Output is correct |
27 |
Correct |
92 ms |
22464 KB |
Output is correct |
28 |
Correct |
87 ms |
16636 KB |
Output is correct |
29 |
Correct |
84 ms |
16736 KB |
Output is correct |
30 |
Correct |
80 ms |
17456 KB |
Output is correct |
31 |
Correct |
81 ms |
16272 KB |
Output is correct |
32 |
Correct |
61 ms |
15716 KB |
Output is correct |
33 |
Correct |
4 ms |
12892 KB |
Output is correct |
34 |
Correct |
3 ms |
13144 KB |
Output is correct |
35 |
Correct |
4 ms |
12892 KB |
Output is correct |
36 |
Correct |
4 ms |
12764 KB |
Output is correct |
37 |
Correct |
90 ms |
17828 KB |
Output is correct |
38 |
Correct |
96 ms |
17880 KB |
Output is correct |
39 |
Correct |
97 ms |
16844 KB |
Output is correct |
40 |
Correct |
98 ms |
16688 KB |
Output is correct |
41 |
Correct |
91 ms |
16840 KB |
Output is correct |
42 |
Correct |
104 ms |
17084 KB |
Output is correct |
43 |
Correct |
92 ms |
17228 KB |
Output is correct |
44 |
Correct |
103 ms |
17104 KB |
Output is correct |
45 |
Correct |
94 ms |
17140 KB |
Output is correct |
46 |
Correct |
98 ms |
17100 KB |
Output is correct |
47 |
Correct |
89 ms |
16588 KB |
Output is correct |
48 |
Correct |
90 ms |
17356 KB |
Output is correct |
49 |
Correct |
94 ms |
18772 KB |
Output is correct |
50 |
Correct |
84 ms |
18780 KB |
Output is correct |
51 |
Correct |
15 ms |
14944 KB |
Output is correct |
52 |
Correct |
104 ms |
22476 KB |
Output is correct |
53 |
Correct |
93 ms |
16628 KB |
Output is correct |
54 |
Correct |
99 ms |
16724 KB |
Output is correct |
55 |
Correct |
87 ms |
17436 KB |
Output is correct |
56 |
Correct |
88 ms |
16340 KB |
Output is correct |
57 |
Correct |
68 ms |
15648 KB |
Output is correct |
58 |
Correct |
3 ms |
12892 KB |
Output is correct |
59 |
Correct |
4 ms |
12892 KB |
Output is correct |
60 |
Correct |
3 ms |
12892 KB |
Output is correct |
61 |
Correct |
4 ms |
12888 KB |
Output is correct |
62 |
Correct |
4 ms |
12764 KB |
Output is correct |
63 |
Correct |
4 ms |
12764 KB |
Output is correct |
64 |
Correct |
4 ms |
12916 KB |
Output is correct |
65 |
Correct |
114 ms |
23520 KB |
Output is correct |
66 |
Correct |
93 ms |
18380 KB |
Output is correct |
67 |
Correct |
92 ms |
23968 KB |
Output is correct |
68 |
Correct |
15 ms |
14820 KB |
Output is correct |
69 |
Correct |
3 ms |
12892 KB |
Output is correct |
70 |
Correct |
3 ms |
12892 KB |
Output is correct |
71 |
Correct |
4 ms |
12760 KB |
Output is correct |
72 |
Correct |
4 ms |
12892 KB |
Output is correct |
73 |
Correct |
3 ms |
12760 KB |
Output is correct |
74 |
Correct |
4 ms |
12892 KB |
Output is correct |
75 |
Correct |
3 ms |
12892 KB |
Output is correct |
76 |
Correct |
5 ms |
12756 KB |
Output is correct |
77 |
Correct |
3 ms |
12888 KB |
Output is correct |
78 |
Correct |
3 ms |
12892 KB |
Output is correct |
79 |
Correct |
3 ms |
12892 KB |
Output is correct |
80 |
Correct |
3 ms |
12884 KB |
Output is correct |
81 |
Correct |
4 ms |
12892 KB |
Output is correct |
82 |
Correct |
3 ms |
12892 KB |
Output is correct |
83 |
Correct |
4 ms |
12892 KB |
Output is correct |