-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathF.cpp
90 lines (89 loc) · 2.87 KB
/
F.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <bits/stdc++.h>
using namespace std;
#define il inline
#define ptc putchar
#define pb push_back
#define R(i, l, r) for (int i = l; i <= r; ++i)
#define debug puts("--------------------------------------------")
typedef long long ll;
typedef pair<int, int> PII;
namespace ZeroTwo {
template <typename T>
il void read(T &x) {
x = 0; T f = 1; char ch;
while (!isdigit(ch = getchar())) f -= (ch == '-') << 1;
while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch & 15), ch = getchar();
x *= f;
}
template <typename T, typename ...L>
il void read(T &x, L &...y) {read(x); read(y...);}
template <typename T>
il void write(T x) {
if (x < 0) ptc('-'), x = -x;
if (x > 9) write(x / 10);
ptc(x % 10 + '0');
}
template <typename T, typename ...L>
il void write(T &x, L &...y) {write(x), ptc(' '); write(y...);}
}
using namespace ZeroTwo;
const int N = 5005;
int n, f[N][N][2]; // 以i为根的子树内,有j个点染成了黑色,当前点是(0/1)白黑,此时的最小两端不同色的边数
int g[N][N], tot, sz[N];
vector <int> E[N];
void init(int x, int fa) {
sz[x] = 1;
for (auto v : E[x]) {
if (v == fa) continue;
init(v, x);
sz[x] += sz[v];
}
}
void dfs(int x, int fa) {
// cout << x << endl;
if (E[x].size() == 1) {
R(j, 0, sz[x])
R(k, 0, 1) f[x][j][k] = INT_MAX / 2;
f[x][1][1] = 0;
f[x][0][0] = 0;
return ;
}
bool flg = 0;
for (int v : E[x]) {
if (v == fa) continue;
dfs(v, x);
R(j, 0, sz[x])
R(k, 0, 1) g[j][k] = f[x][j][k], f[x][j][k] = INT_MAX / 2;
if (!flg) {
R(j, 0, sz[x]) R(k, 0, 1) g[j][k] = INT_MAX / 2;
g[0][0] = g[0][1] = 0;
flg = 1;
}
R(j, 0, sz[x]) {
R(k, 0, min(sz[v], tot / 2 - j)) {
// cout << f[x][j + k][0] << endl;
f[x][j + k][0] = min(g[j][0] + min(f[v][k][1] + 1, f[v][k][0]), f[x][j + k][0]);
f[x][j + k][1] = min(g[j][1] + min(f[v][k][0] + 1, f[v][k][1]), f[x][j + k][1]);
// if (x == 1) cout << j << ' '<< k << ' '<< g[j][0] << ' ' << f[v][k][1] << ' ' << f[v][k][0] << ' ' << f[x][j + k][0] << endl;
// cout << f[x][j + k][0] << endl;
}
}
// R(j, 0, tot / 2) cout << x << ' '<< j << ' '<< f[x][j][0] << endl;
}
}
signed main() {
cin >> n;
R(i, 1, n - 1) {
int u, v; cin >> u >> v;
E[u].push_back(v), E[v].push_back(u);
}
int root = 0;
R(i, 1, n) {
if (E[i].size() > 1) root = i;
else ++tot;
}
init(root, 0);
dfs(root, 0);
cout << min(f[root][tot / 2][0], f[root][tot / 2][1]) << endl;
return 0;
}