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
output
input
output
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; }
相关文章推荐
- iOS8中使用CoreLocation定位
- 做简单的集群SSH免密码配置(只需要在1台机器操作!只需要2步!)
- activity生命周期
- mysql数据库管理工具sqlyog在首选项里可以设置默认查询分页条数和字体,改写关键字大小写
- Demo Nec
- ubuntu 用户sudo组不小心被删除解决方案
- 解决程序提示“应用程序发生异常 未知的软件异常(0x0eedfade),位置为 0x7c812fd3”
- Ajax与HTML5 history pushState/replaceState实例
- 基于注解的 Spring MVC 简单入门
- 简单自动补全(js+css)
- pyspider—爬取下载图片
- 数据结构广义表实验
- [Error]Win8安装程序出现2502、2503错误解决方法
- .net学习笔记---lambda表达式(自执行方法)
- JAVA8函数式编程
- 浅析nginx刚刚发布的JavaScript能力nginScript
- linux下dns设置详解
- Active Learning 主动学习
- 四大配置管理工具测评手记
- 手势识别——GestureDetector