poj 2378 Tree Cutting 树形DP
来源:互联网 发布:mindmap中文版软件 编辑:程序博客网 时间:2024/06/07 07:05
Tree CuttingTime Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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).
题意:给出一个节点树为n的树,要求找出一些节点,当删去这个节点以及它所连接的边后,剩下的每一个部分的节点树都不大于n/2,从小到大输出满足条件的节点。
思路:很容易想到,当删除一个节点p后,会出现:
1.除了以p节点为根的子树的部分,这部分的节点数为n-(以p节点为根的子树的节点树)
2.以p节点的每一个孩子为根的子树
如果这些部分全都满足节点数不大于n,那么p节点自然是满足条件的
我们首先需要建树,我采用vector方法,保证不超内存,用邻接表也可以,然后dfs遍历这棵树,计算以每一个节点为根的子树中节点的数量,并判断,将满足条件的节点做标记,最后按顺序输出即可
#include <iostream>#include <stdio.h>#include <vector>#include <string.h>using namespace std;vector<int> p[10010];int nodenum[10010];bool vis[10010];bool res[10010];int n,half;void init(){ memset(vis,false,sizeof(vis)); memset(res,false,sizeof(vis));}int dfs(int x){ bool ok=true; nodenum[x]=1; int num=p[x].size(); int i; for(i=0;i<num;i++) { if(!vis[p[x][i]]) { vis[x]=true; int tmp=dfs(p[x][i]); if(tmp>half) ok=false; nodenum[x]+=tmp; } } if(ok&&n-nodenum[x]<=half) res[x]=true; return nodenum[x];}int main(){ int u,v,i; while(~scanf("%d",&n)) { init(); half=n/2; for(i=0;i<n-1;i++) { scanf("%d%d",&u,&v); p[u].push_back(v); p[v].push_back(u); } vis[1]=true; dfs(1); for(i=1;i<=n;i++) { if(res[i]) printf("%d\n",i); } } return 0;}
0 0
- 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 (简单树形DP、删点)
- POJ.2378 Tree Cutting (树形DP 树的重心)
- POJ 2378 Tree Cutting——树形dp
- Tree Cutting (树形DP)
- Tree Cutting 树形dp
- 论文阅读笔记:vTPM
- 栈的应用 平衡符号 后缀表达式 中缀到后缀的转换
- Android 图标上面添加提醒(二)使用开源UI类库 Viewbadger
- 逆波兰表达式
- 【HDU】 1003 Max Sum
- poj 2378 Tree Cutting 树形DP
- Swift - 回调机制
- git使用
- php获取后缀名
- POJ 2342 Anniversary party (树形dp)
- Eclipse.ini配置文件
- asdasd
- 分页查询
- Fragment——向Activity中动态加载Fragment