Poj 2378 Tree Cutting 树型DP
来源:互联网 发布:网络与新媒体概论 资源 编辑:程序博客网 时间:2024/06/04 01:00
Tree Cutting
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4594 Accepted: 2808
Description
After Farmer John realized that Bessie had installed a "tree-shaped" network among his N (1 <= N <= 10,000) barns at an incredible cost, he sued Bessie to mitigate his losses.
Bessie, feeling vindictive, decided to sabotage Farmer John's network by cutting power to one of the barns (thereby disrupting all the connections involving that barn). When Bessie does this, it breaks the network into smaller pieces, each of which retains full connectivity within itself. In order to be as disruptive as possible, Bessie wants to make sure that each of these pieces connects together no more than half the barns on FJ.
Please help Bessie determine all of the barns that would be suitable to disconnect.
Bessie, feeling vindictive, decided to sabotage Farmer John's network by cutting power to one of the barns (thereby disrupting all the connections involving that barn). When Bessie does this, it breaks the network into smaller pieces, each of which retains full connectivity within itself. In order to be as disruptive as possible, Bessie wants to make sure that each of these pieces connects together no more than half the barns on FJ.
Please help Bessie determine all of the barns that would be suitable to disconnect.
Input
* Line 1: A single integer, N. The barns are numbered 1..N.
* Lines 2..N: Each line contains two integers X and Y and represents a connection between barns X and Y.
* Lines 2..N: Each line contains two integers X and Y and represents a connection between barns X and Y.
Output
* Lines 1..?: Each line contains a single integer, the number (from 1..N) of a barn whose removal splits the network into pieces each having at most half the original number of barns. Output the barns in increasing numerical order. If there are no suitable barns, the output should be a single line containing the word "NONE".
Sample Input
101 22 33 44 56 77 88 99 103 8
Sample Output
38
Hint
INPUT DETAILS:
The set of connections in the input describes a "tree": it connects all the barns together and contains no cycles.
OUTPUT DETAILS:
If barn 3 or barn 8 is removed, then the remaining network will have one piece consisting of 5 barns and two pieces containing 2 barns. If any other barn is removed then at least one of the remaining pieces has size at least 6 (which is more than half of the original number of barns, 5).
The set of connections in the input describes a "tree": it connects all the barns together and contains no cycles.
OUTPUT DETAILS:
If barn 3 or barn 8 is removed, then the remaining network will have one piece consisting of 5 barns and two pieces containing 2 barns. If any other barn is removed then at least one of the remaining pieces has size at least 6 (which is more than half of the original number of barns, 5).
Source
USACO 2004 December Silver
较简单的树型DP,计算每个节点子树的最大节点数以及去掉这个节点和它的子树后的节点数,看是否超过总节点数的一半。
#include <cstdio>#include <iostream>#include <vector>#include <string.h>const int maxn=10005;using namespace std;vector<int> v[maxn];int a[maxn],visit[maxn],sum[maxn];int n;int dfs(int now) {visit[now]=1;int f;for (int i=0;i<v[now].size();i++) {int to=v[now][i];if (!visit[to]) {f=dfs(to);a[now]=max(a[now],f);sum[now]+=f;}}a[now]=max(a[now],n-sum[now]-1);return sum[now]+1;}int main() {int i,x,y;memset(visit,0,sizeof(visit));memset(sum,0,sizeof(sum));memset(a,0,sizeof(a));scanf("%d",&n);for (i=1;i<n;i++) {scanf("%d%d",&x,&y);v[y].push_back(x);v[x].push_back(y);}int u=dfs(1),cnt=0;for (i=1;i<=n;i++) {if (a[i]<=n/2) cnt++,printf("%d\n",i);}if (!cnt) printf("NONE");return 0;}
阅读全文
0 0
- Poj 2378 Tree Cutting 树型DP
- (POJ 2378 Tree Cutting)树型DP + 删点
- POJ--2378--Tree Cutting--树形DP
- poj 2378 Tree Cutting(树形dp)
- (简单) 树形dp POJ 2378 Tree Cutting
- poj 2378 Tree Cutting (树形dp)
- poj 2378 Tree Cutting 树形dp
- Tree Cutting - POJ 2378 树形dp
- POJ 2378 Tree Cutting(树形DP)
- poj 2378 Tree Cutting 树状dp
- poj 2378 Tree Cutting (树形dp)
- poj 2378 Tree Cutting(树形dp)
- poj 2378 Tree Cutting 树形DP
- Poj 2378 Tree Cutting (树形DP)
- Poj 2378 Tree Cutting(树形dp)
- poj 2378 Tree Cutting
- POJ 2378 Tree Cutting
- POJ 2378 Tree Cutting
- Android 动画学习笔记(一)
- 一篇就够了系列之ContentProvider全解析
- 《集体智慧编程》笔记二
- Android手机文件系统操作——轻松存储与获取手机文件
- 安装完appserv之后数据库无法登陆问题解决办法
- Poj 2378 Tree Cutting 树型DP
- QT之布局篇
- 打飞机html + js + canvas
- Linux 常用性能命令
- 1
- 自定义view系列(7)--SwitchView
- imx6 板卡移植官方yocto版本(1_安装环境)
- maven使用java 需在jre配置中加上
- 2