#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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
11988 KB |
Output is correct |
2 |
Correct |
5 ms |
11988 KB |
Output is correct |
3 |
Correct |
5 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
12024 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
11988 KB |
Output is correct |
9 |
Correct |
5 ms |
11988 KB |
Output is correct |
10 |
Correct |
5 ms |
11988 KB |
Output is correct |
11 |
Correct |
8 ms |
11988 KB |
Output is correct |
12 |
Correct |
6 ms |
12032 KB |
Output is correct |
13 |
Correct |
5 ms |
12028 KB |
Output is correct |
14 |
Correct |
5 ms |
11988 KB |
Output is correct |
15 |
Correct |
6 ms |
11988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
11980 KB |
Output is correct |
2 |
Correct |
5 ms |
11988 KB |
Output is correct |
3 |
Correct |
5 ms |
11988 KB |
Output is correct |
4 |
Correct |
107 ms |
23420 KB |
Output is correct |
5 |
Correct |
105 ms |
18360 KB |
Output is correct |
6 |
Correct |
107 ms |
24072 KB |
Output is correct |
7 |
Correct |
18 ms |
14156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
109 ms |
17424 KB |
Output is correct |
2 |
Correct |
107 ms |
18568 KB |
Output is correct |
3 |
Correct |
98 ms |
18688 KB |
Output is correct |
4 |
Correct |
18 ms |
14116 KB |
Output is correct |
5 |
Correct |
110 ms |
22364 KB |
Output is correct |
6 |
Correct |
104 ms |
16516 KB |
Output is correct |
7 |
Correct |
106 ms |
16648 KB |
Output is correct |
8 |
Correct |
110 ms |
17300 KB |
Output is correct |
9 |
Correct |
97 ms |
16132 KB |
Output is correct |
10 |
Correct |
78 ms |
15436 KB |
Output is correct |
11 |
Correct |
6 ms |
11988 KB |
Output is correct |
12 |
Correct |
6 ms |
12024 KB |
Output is correct |
13 |
Correct |
6 ms |
12084 KB |
Output is correct |
14 |
Correct |
7 ms |
11988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
11988 KB |
Output is correct |
2 |
Correct |
5 ms |
11988 KB |
Output is correct |
3 |
Correct |
5 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
12024 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
11988 KB |
Output is correct |
9 |
Correct |
5 ms |
11988 KB |
Output is correct |
10 |
Correct |
5 ms |
11988 KB |
Output is correct |
11 |
Correct |
8 ms |
11988 KB |
Output is correct |
12 |
Correct |
6 ms |
12032 KB |
Output is correct |
13 |
Correct |
5 ms |
12028 KB |
Output is correct |
14 |
Correct |
5 ms |
11988 KB |
Output is correct |
15 |
Correct |
6 ms |
11988 KB |
Output is correct |
16 |
Correct |
5 ms |
11980 KB |
Output is correct |
17 |
Correct |
5 ms |
11988 KB |
Output is correct |
18 |
Correct |
5 ms |
11988 KB |
Output is correct |
19 |
Correct |
107 ms |
23420 KB |
Output is correct |
20 |
Correct |
105 ms |
18360 KB |
Output is correct |
21 |
Correct |
107 ms |
24072 KB |
Output is correct |
22 |
Correct |
18 ms |
14156 KB |
Output is correct |
23 |
Correct |
109 ms |
17424 KB |
Output is correct |
24 |
Correct |
107 ms |
18568 KB |
Output is correct |
25 |
Correct |
98 ms |
18688 KB |
Output is correct |
26 |
Correct |
18 ms |
14116 KB |
Output is correct |
27 |
Correct |
110 ms |
22364 KB |
Output is correct |
28 |
Correct |
104 ms |
16516 KB |
Output is correct |
29 |
Correct |
106 ms |
16648 KB |
Output is correct |
30 |
Correct |
110 ms |
17300 KB |
Output is correct |
31 |
Correct |
97 ms |
16132 KB |
Output is correct |
32 |
Correct |
78 ms |
15436 KB |
Output is correct |
33 |
Correct |
6 ms |
11988 KB |
Output is correct |
34 |
Correct |
6 ms |
12024 KB |
Output is correct |
35 |
Correct |
6 ms |
12084 KB |
Output is correct |
36 |
Correct |
7 ms |
11988 KB |
Output is correct |
37 |
Correct |
110 ms |
17844 KB |
Output is correct |
38 |
Correct |
104 ms |
17944 KB |
Output is correct |
39 |
Correct |
109 ms |
16912 KB |
Output is correct |
40 |
Correct |
106 ms |
16704 KB |
Output is correct |
41 |
Correct |
103 ms |
16624 KB |
Output is correct |
42 |
Correct |
108 ms |
17032 KB |
Output is correct |
43 |
Correct |
105 ms |
17028 KB |
Output is correct |
44 |
Correct |
105 ms |
17028 KB |
Output is correct |
45 |
Correct |
114 ms |
17072 KB |
Output is correct |
46 |
Correct |
106 ms |
17104 KB |
Output is correct |
47 |
Correct |
100 ms |
16288 KB |
Output is correct |
48 |
Correct |
111 ms |
17376 KB |
Output is correct |
49 |
Correct |
108 ms |
18684 KB |
Output is correct |
50 |
Correct |
98 ms |
18688 KB |
Output is correct |
51 |
Correct |
18 ms |
14108 KB |
Output is correct |
52 |
Correct |
110 ms |
22404 KB |
Output is correct |
53 |
Correct |
110 ms |
16520 KB |
Output is correct |
54 |
Correct |
109 ms |
16672 KB |
Output is correct |
55 |
Correct |
102 ms |
17360 KB |
Output is correct |
56 |
Correct |
96 ms |
16096 KB |
Output is correct |
57 |
Correct |
75 ms |
15488 KB |
Output is correct |
58 |
Correct |
6 ms |
12032 KB |
Output is correct |
59 |
Correct |
6 ms |
11988 KB |
Output is correct |
60 |
Correct |
5 ms |
12028 KB |
Output is correct |
61 |
Correct |
6 ms |
11988 KB |
Output is correct |
62 |
Correct |
6 ms |
11988 KB |
Output is correct |
63 |
Correct |
6 ms |
12080 KB |
Output is correct |
64 |
Correct |
6 ms |
12160 KB |
Output is correct |
65 |
Correct |
108 ms |
23424 KB |
Output is correct |
66 |
Correct |
110 ms |
18500 KB |
Output is correct |
67 |
Correct |
112 ms |
24004 KB |
Output is correct |
68 |
Correct |
19 ms |
14204 KB |
Output is correct |
69 |
Correct |
6 ms |
11988 KB |
Output is correct |
70 |
Correct |
6 ms |
12072 KB |
Output is correct |
71 |
Correct |
6 ms |
12116 KB |
Output is correct |
72 |
Correct |
6 ms |
11988 KB |
Output is correct |
73 |
Correct |
5 ms |
11988 KB |
Output is correct |
74 |
Correct |
6 ms |
11988 KB |
Output is correct |
75 |
Correct |
6 ms |
11988 KB |
Output is correct |
76 |
Correct |
6 ms |
11988 KB |
Output is correct |
77 |
Correct |
6 ms |
12032 KB |
Output is correct |
78 |
Correct |
5 ms |
12032 KB |
Output is correct |
79 |
Correct |
6 ms |
12032 KB |
Output is correct |
80 |
Correct |
6 ms |
11988 KB |
Output is correct |
81 |
Correct |
6 ms |
11988 KB |
Output is correct |
82 |
Correct |
6 ms |
11988 KB |
Output is correct |
83 |
Correct |
6 ms |
12116 KB |
Output is correct |