Codeforces 581F Zublicanes and Mumocrates - 树形动态规划

时间:2021-09-17 03:22:49

It's election time in Berland. The favorites are of course parties of zublicanes and mumocrates. The election campaigns of both parties include numerous demonstrations on n main squares of the capital of Berland. Each of the n squares certainly can have demonstrations of only one party, otherwise it could lead to riots. On the other hand, both parties have applied to host a huge number of demonstrations, so that on all squares demonstrations must be held. Now the capital management will distribute the area between the two parties.

Some pairs of squares are connected by (n - 1) bidirectional roads such that between any pair of squares there is a unique way to get from one square to another. Some squares are on the outskirts of the capital meaning that they are connected by a road with only one other square, such squares are called dead end squares.

The mayor of the capital instructed to distribute all the squares between the parties so that the dead end squares had the same number of demonstrations of the first and the second party. It is guaranteed that the number of dead end squares of the city is even.

To prevent possible conflicts between the zublicanes and the mumocrates it was decided to minimize the number of roads connecting the squares with the distinct parties. You, as a developer of the department of distributing squares, should determine this smallest number.

Input

The first line of the input contains a single integer n (2 ≤ n ≤ 5000) — the number of squares in the capital of Berland.

Next n - 1 lines contain the pairs of integers x, y (1 ≤ x, y ≤ n, x ≠ y) — the numbers of the squares connected by the road. All squares are numbered with integers from 1 to n. It is guaranteed that the number of dead end squares of the city is even.

Output

Print a single number — the minimum number of roads connecting the squares with demonstrations of different parties.

Examples
input
8
1 4
2 4
3 4
6 5
7 5
8 5
4 5
output
1
input
5
1 2
1 3
1 4
1 5
output
2

题目大意

  给定一棵有$n$个点的无根树(度为1的点不为根),保证它的叶节点的个数为偶数。将所有点染成黑白两种颜色,要求

  1. 黑的叶节点数等于白的叶节点数
  2. 有边相连但颜色不同的点对数最少

  问最少的这样的点对数。

  显然动态规划。

Solution 1

  用$f[i][j][0/1]$表示当前考虑$i$号点,它的子树内有$j$个叶节点是黑色的最优结果。

  转移是显然的。

  至于时间复杂度为什么可过?下面解释一下(为了方便计算,那么就用子树$size$来说明吧)

  设当前考虑的节点的第$i$个子节点为$s_{i}$。

  $\sum_{i = 1}size[s_{i}]\cdot\sum_{j = 1} ^ {i - 1}size[s_{j}] = \sum_{i < j}size[s_{i}]\cdot size[s_{j}]$

  然后可以发现对于任意一对节点$\left(u, v\right)$仅对它们的lca有1的贡献,所以总时间复杂度为$O\left(n^{2}\right)$

Code

 /**
* Codeforces
* Problem#581F
* Accepted
* Time: 139ms
* Memory: 198380k
*/
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean;
#define smin(_a, _b) (_a = min(_a, _b)) const int N = ; int n;
vector<int> *g;
// 0: black, 1: white
int f[N][N][]; // node, number of the black nodes, the color of this node
int temp[N][];
int root;
int clf[N];
int deg[N]; inline void init() {
scanf("%d", &n);
g = new vector<int>[(n + )];
for(int i = , u, v; i < n; i++) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
deg[u]++, deg[v]++;
}
for(root = ; root < n && deg[root] == ; root++);
} void treedp(int node, int fa) {
if(deg[node] == ) {
f[node][][] = f[node][][] = ;
clf[node] = ;
return;
}
// memset(temp)
clf[node] = ;
f[node][][] = f[node][][] = ;
for (int i = ; i < deg[node]; i++) {
int e = g[node][i];
if (e == fa) continue;
treedp(e, node);
memset(temp, 0x3f, sizeof(temp));
for (int s1 = clf[node]; ~s1; s1--) {
for (int s2 = clf[e]; ~s2; s2--) {
smin(temp[s1 + s2][], f[node][s1][] + min(f[e][s2][], f[e][s2][] + ));
smin(temp[s1 + s2][], f[node][s1][] + min(f[e][s2][] + , f[e][s2][]));
}
}
clf[node] += clf[e];
for (int j = ; j <= clf[node]; j++)
f[node][j][] = temp[j][], f[node][j][] = temp[j][];
}
} inline void solve() {
memset(f, 0x3f, sizeof(f));
treedp(root, );
int k = clf[root] >> ;
int ans = min(f[root][k][], f[root][k][]);
printf("%d\n", ans);
} int main() {
init();
solve();
return ;
}

Slower Version

Solution 2

  由于转移的时候仅和当前节点的颜色和它的父节点的颜色是否相同有关,所以用$f[i][j]$表示当前考虑第$i$号点,它的子树内有$j$个叶节点是黑色的最优结果。

  怎么转移呢?

  先当父节点颜色和当前节点颜色相同,按照上面的方法进行转移。

  然后考虑将当前子树内的所有点的颜色反转,这样会导致当前点和父节点的颜色不同,答案加1,这样去更新。

Code

 /**
* Codeforces
* Problem#581F
* Accepted
* Time: 61ms
* Memory: 100280k
*/
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean;
#define smin(_a, _b) (_a = min(_a, _b)) const int N = ; int n;
vector<int> g[N];
int f[N][N]; // node, number of the black nodes
int root;
int clf[N];
int deg[N]; inline void init() {
scanf("%d", &n);
for(int i = , u, v; i < n; i++) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
deg[u]++, deg[v]++;
}
for(root = ; root < n && deg[root] == ; root++);
} void treedp(int node, int fa) {
if(deg[node] == ) {
f[node][] = , f[node][] = ;
clf[node] = ;
return;
}
clf[node] = ;
f[node][] = ;
for (int i = ; i < deg[node]; i++) {
int e = g[node][i];
if (e == fa) continue;
treedp(e, node);
for (int s1 = clf[node]; ~s1; s1--) {
for (int s2 = clf[e]; ~s2; s2--) {
smin(f[node][s1 + s2], f[node][s1] + f[e][s2]);
}
}
clf[node] += clf[e];
}
for (int i = ; i <= clf[node]; i++)
smin(f[node][i], f[node][clf[node] - i] + ); // reverse the color of each node
} inline void solve() {
memset(f, 0x3f, sizeof(f));
treedp(root, );
int k = clf[root] >> ;
printf("%d\n", f[root][k]);
} int main() {
init();
solve();
return ;
}