您的位置:首页 > 其它

CF 581F 树形dp

2015-09-30 14:51 288 查看
http://codeforces.com/problemset/problem/581/F

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

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


/**
CF 581F  树形dp
题目大意:把一棵树中的叶子节点平均分成两份,使得两份中的叶子节点不能有边直接或间接相连,问最少要删掉多少条边。
解题思路:dp[u][0][i]表示以u为根节点的子树把i个叶子节点分出去,其中并不包括节点u时需要删的最少边数
dp[u][1][i]表示以u为根节点的子树把i个叶子节点分出去,其中不包括节点u时需要删的最少边数
状态转移见代码,注意边界的处理,整棵树的根节点不能是叶子节点(度数为1)
*/

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=5030;
int head[maxn],ip;
void init()
{
memset(head,-1,sizeof(head));
ip=0;
}
struct note
{
int v,next;
} edge[maxn*2];
void addedge(int u,int v)
{
edge[ip].v=v,edge[ip].next=head[u],head[u]=ip++;
}

int dp[maxn][2][maxn],n,c[2][maxn],siz[maxn],du[maxn];

void dfs(int u,int pre)
{
if(siz[u] == 1)
dp[u][0][1] = 0, dp[u][1][0] = 0;
else
dp[u][0][0] = dp[u][1][0] = 0;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
if(v==pre)continue;
dfs(v,u);
for(int i=0; i<=n; i++)c[0][i]=c[1][i]=INF;
for(int i=0; i<=siz[u]; i++)
{
for(int j=0; j<=siz[v]; j++)
{
c[0][i+j]=min(c[0][i+j],dp[u][0][i]+dp[v][0][j]);
c[1][i+j]=min(c[1][i+j],dp[u][1][i]+dp[v][1][j]);
c[0][i+j]=min(c[0][i+j],dp[u][0][i]+dp[v][1][j]+1);
c[1][i+j]=min(c[1][i+j],dp[u][1][i]+dp[v][0][j]+1);
}
}
siz[u]+=siz[v];
for(int i=0; i<=siz[u]+siz[v]; i++)dp[u][0][i]=c[0][i],dp[u][1][i]=c[1][i];
}
}
int main()
{
while(~scanf("%d",&n))
{
init();
memset(du,0,sizeof(du));
for(int i=1; i<n; i++)
{
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
du[u]++;
du[v]++;
}
int root=1;
for(int i=1; i<=n; i++)
{
if(du[i]==1)
siz[i]=1;
else
root=i,siz[i]=0;
}
memset(dp,INF,sizeof(dp));
dfs(root,-1);
printf("%d\n",min(dp[root][0][siz[root]/2],dp[root][1][siz[root]/2]));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: