#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e5 + 2;
const int inf = 2e9;
struct Dsu {
int p[N];
void Init(int n) {
for (int i = 1; i <= n; i++) p[i] = i;
}
int Find(int x) {
if (x == p[x]) return x;
return p[x] = Find(p[x]);
}
void Unite(int u, int v) {
u = Find(u); v = Find(v);
p[v] = u;
}
}dsu;
vector<int> g[N];
int sta[N], col[N];
int dp[N][2][2];
int Resi(int s, int e) {
if (sta[s] == 0) {
dp[s][col[s]][0] = col[s];
int mx = 0;
for (int u : g[s]) {
if (u == e) continue;
Resi(u, s);
smax(mx, dp[u][0][col[s]]);
dp[s][col[s]][0] += dp[u][0][col[s]];
}
if (mx == inf) dp[s][col[s]][0] = mx;
return dp[s][col[s]][0];
}
for (int u : g[s]) if (u != e) Resi(u, s);
for (int j = 0; j < 2; j++) {
if (col[s] < 2 && col[s] != j) continue;
dp[s][j][0] = dp[s][j][1] = j;
int sum = 0;
int mx = 0;
int cnt = 0;
for (int u : g[s]) {
if (u == e) continue;
if (dp[u][0][j] == inf) {mx = u; cnt += 1;}
if (mx != u) sum += dp[u][0][j];
if (mx != u)dp[s][j][0] += dp[u][0][j];
if (mx != u)dp[s][j][1] += dp[u][0][j];
}
if (cnt >= 2) {
dp[s][j][0] = dp[s][j][1] = inf;
continue;
}
if (cnt == 1) {
dp[s][j][1] = inf;
if (dp[mx][1][j] != inf) dp[s][j][0] += dp[mx][1][j];
else dp[s][j][0] = inf;
continue;
}
mx = -inf;
for (int u : g[s]) {
if (u == e) continue;
if (dp[u][1][j] != inf) smax(mx, dp[u][0][j] - dp[u][1][j]);
}
if (mx == -inf) dp[s][j][0] = inf;
else dp[s][j][0] -= mx;
}
return min({dp[s][0][0], dp[s][1][0]});
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n; cin >> n;
dsu.Init(n);
int A, B; A = B = 0;
for (int i = 0; i < n; i++) {
int u, v;
cin >> u >> v;
if (dsu.Find(u) != dsu.Find(v)) {
dsu.Unite(u, v);
g[u].push_back(v);
g[v].push_back(u);
}
else {
A = u;
B = v;
}
}
for (int i = 1; i <= n; i++) {
sta[i] = 1; col[i] = 2;
}
for (int i = 1; i <= n; i++) for (int j = 0; j < 4; j++) dp[i][j / 2][j % 2] = inf;
col[A] = col[B] = 0;
int ans = Resi(1, 0);
for (int i = 1; i <= n; i++) for (int j = 0; j < 4; j++) dp[i][j / 2][j % 2] = inf;
col[B] = 1; sta[A] = 0;
smin(ans, Resi(1, 0));
for (int i = 1; i <= n; i++) for (int j = 0; j < 4; j++) dp[i][j / 2][j % 2] = inf;
col[A] = 1; sta[B] = 0;
smin(ans, Resi(1, 0));
for (int i = 1; i <= n; i++) for (int j = 0; j < 4; j++) dp[i][j / 2][j % 2] = inf;
col[B] = 0; sta[A] = 1;
smin(ans, Resi(1, 0));
if (ans == inf) ans = -1;
cout << ans << en;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
96 ms |
18284 KB |
Output is correct |
6 |
Correct |
101 ms |
18012 KB |
Output is correct |
7 |
Correct |
134 ms |
15832 KB |
Output is correct |
8 |
Correct |
108 ms |
16448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2684 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
2 ms |
2808 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2684 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
2 ms |
2808 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2684 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2684 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2772 KB |
Output is correct |
24 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
96 ms |
18284 KB |
Output is correct |
6 |
Correct |
101 ms |
18012 KB |
Output is correct |
7 |
Correct |
134 ms |
15832 KB |
Output is correct |
8 |
Correct |
108 ms |
16448 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2688 KB |
Output is correct |
12 |
Correct |
2 ms |
2684 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
1 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2688 KB |
Output is correct |
19 |
Correct |
2 ms |
2808 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2684 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
2 ms |
2684 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
2 ms |
2772 KB |
Output is correct |
31 |
Correct |
2 ms |
2772 KB |
Output is correct |
32 |
Correct |
2 ms |
2772 KB |
Output is correct |
33 |
Correct |
71 ms |
9004 KB |
Output is correct |
34 |
Correct |
92 ms |
8912 KB |
Output is correct |
35 |
Correct |
62 ms |
8996 KB |
Output is correct |
36 |
Correct |
63 ms |
9000 KB |
Output is correct |
37 |
Correct |
13 ms |
4304 KB |
Output is correct |
38 |
Correct |
65 ms |
9048 KB |
Output is correct |
39 |
Correct |
6 ms |
3156 KB |
Output is correct |
40 |
Correct |
79 ms |
8948 KB |
Output is correct |
41 |
Correct |
38 ms |
10228 KB |
Output is correct |
42 |
Correct |
50 ms |
10004 KB |
Output is correct |
43 |
Correct |
107 ms |
15872 KB |
Output is correct |
44 |
Correct |
106 ms |
14532 KB |
Output is correct |