CodeForces 581F Zublicanes and Mumocrates 树形DP

给出一棵树,要求将树叶染成2种颜色并使这两种颜色之间不连通,求最少删边数。

树D,令f[i][j]表示i的子树分走j个叶子的最小删边数。搞下背包。

一定要从非叶节点开始dp。。

写了一些冗余代码了。。

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 5001;
vector<int> adj[N];
int sz[N], f[N][N];
int n;

void dfs(int u, int fa) {
    if (adj[u].size() == 1) sz[u] = 1;
    int i, v, p, j;
    for (i = 1; i <= n; i++) f[u][i] = 0x3f3f3f3f;
    for (p = 0; p < adj[u].size(); p++) {
        v = adj[u][p];
        if (v == fa) continue;
        dfs(v, u);
        sz[u] += sz[v];
    }
    for (p = 0; p < adj[u].size(); p++) {
        v = adj[u][p];
        if (v == fa) continue;
        for(int i = sz[u]; i >= 0; i--)
            for(int j = 0; j <= sz[v]; j++)
                if(i + j <= sz[u])
                    f[u][i + j] = min(f[u][i + j], f[u][i] + f[v][j]);
    }
    for (i = 0; i <= sz[u]; i++)
        f[u][i] = min(f[u][i], f[u][sz[u] - i] + 1);
}

int main() {
    scanf("%d", &n);
    if (n == 2) { puts("1"); return 0;}
    int u, v, i, root = 1;
    for (i = 1; i < n; i++){
        scanf("%d%d", &u, &v);
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    while(adj[root].size() == 1) root++;
    dfs(root, 0);
    printf("%d", f[root][sz[root] / 2]);
    return 0;
}


F. Zublicanes and Mumocrates
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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 thedead 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.

Sample test(s)
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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值