题意:有n个谷仓有n-1条路连接,问最少删除哪几个点才能使得删除点后得到的连通图的加点数不大于n/2.

分析:求树的重心的变形题,poj3107的简单版,一遍dfs从叶子到根转移找出找到以每个节点为根的子树的结点数,f[u]={ f[v1]+f[v2]+.....+f[vn] }+1;使得每棵子树节点数小于n/2,并且父节点得那个连通图节点数小于等于n/2,即n-f[u]<=n/2.

如果没有这样的点,输出NONE

Time limit     1000 ms

Memory limit     65536 kB

OS     Linux

Source      USACO 2004 December Silver

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.

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.

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

10
1 2
2 3
3 4
4 5
6 7
7 8
8 9
9 10
3 8

Sample Output

3
8

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

题意:

给定一棵无向树,节点为n(n<=10000),问删除那些节点可以使得新图中的每一个连通分支的节点数都不超过小于n/2

思路:

树形dp,任意定跟,求出每个节点的儿子节点,每个点为根的子树的节点数dp[i],然后考察每一个点,如果n-dp[i]<=n/2,以及以i的每一个儿子为根的子树的节点数都dp[u]<=n/2,那么这个点就满足条件

/*题意:给了一棵节点数为n的树的对应关系,让判断去掉哪些
节点能使子数的大小小于等于n/2,并将符合条件的节点从小到
大输出,如果没有符合条件的节点,则输出 “NONE”。
思路:既然是让求哪些ji节点符合条件,可以用邻接表构建一个
图,然后用dfs找出每一个节点下连的图的da'x大小些节点符合
条件,将节点存入ans数组中。*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 10010
using namespace std;
int n,s,e,first[N],book[N],ans[N];
struct node
{int x,y;
}que[2*N];
void add(int x,int y) //构建邻接表的函数
{que[e].x=x;que[e].y=first[y];first[y]=e++;
}
int dfs(int x)
{int num=0,sum=0;book[x]=1;int k=first[x],flag=0;while(k!=-1){if(!book[que[k].x]){num=dfs(que[k].x);sum+=num;if(num>n/2)   //x节点下的图的大小超过了n/2,不符合条件flag=1;}k=que[k].y;}if(n-sum-1>n/2)    //另一半是否小于等于n/2flag=1;if(!flag)ans[s++]=x;return sum+1;     //加上自己
}
int main()
{while(~scanf("%d",&n)){memset(first,-1,sizeof(first));//初始化memset(book,0,sizeof(book));s=e=0;   //有s个节点符合条件,e用邻接表表示无向图的大小int a,b;for(int i=1;i<n;i++){scanf("%d%d",&a,&b);add(a,b);       //构建邻接表add(b,a);       //无向图}a=dfs(1);        //从第一个点开始尝试if(s)      //有s个节点符合条件{sort(ans,ans+s);    //从小到大排序,输出for(int i=0;i<s;i++)printf("%d\n",ans[i]);}else    //没有符合条件的节点printf("NONE\n");}
}

Tree Cutting POJ - 2378(树形DP)相关推荐

  1. CodeForces - 1118F1 Tree Cutting (Easy Version)(树形dp)

    题目链接:点击查看 题目大意:给定n个点,每个点的权值分别对应颜色:0:无颜色,1:红色,2:蓝色,现在需要切割边,使切割后的两个部分不能出现红色和蓝色掺杂的部分,也就是说两个部分必须只能各自含有一个 ...

  2. Fire (poj 2152 树形dp)

    Fire (poj 2152 树形dp) 给定一棵n个结点的树(1<n<=1000).现在要选择某些点,使得整棵树都被覆盖到.当选择第i个点的时候,可以覆盖和它距离在d[i]之内的结点,同 ...

  3. Problem F. Grab The Tree HDU - 6324(树形dp+博弈)

    Little Q and Little T are playing a game on a tree. There are nn vertices on the tree, labeled by 1, ...

  4. POJ 3342 树形DP+Hash

    这是很久很久以前做的一道题,可惜当时WA了一页以后放弃了. 今天我又重新捡了起来.(哈哈1A了) 题意: 没有上司的舞会+判重 思路: hash一下+树形DP 题目中给的人名hash到数字,再进行运算 ...

  5. CodeForces - 1092F Tree with Maximum Cost(树形dp+树根转移)

    题目链接:点击查看 题目大意:给出一个树,每个顶点都有一个权值,任意一点到其他点的距离为经过边的数量,求出一点到其他每一个点的距离*权值之和最大 题目分析:树形dp,一开始怎么也想不明白,看了别人的代 ...

  6. poj 2342 树形DP

    树形DP入门题目. 树形DP说白了就是在搜索的时候动态规划. 只要你懂的深搜+动态规划,就能理解这个基础题了. 先直接搜索到最底层,然后一层一层动态规划,可以说有点像数塔. dp[i][1],dp[i ...

  7. poj 2486 树形dp

    思路:这题是裸的树形dp.dp[i][j]表示第i个节点花费j步并且从子节点返回,能得到的最大苹果数:nback[i[j]表示第i个节点花费j步并且进入某个子节点不返回,能得到的最大苹果数.那么我们就 ...

  8. poj 2152 树形dp(建立消防站)

    题意:给定n个节点组成的树,树边有权.现在要在一些点上建立消防站,每个点建站都有花费cost[i].如果某点上没有消防站,那么在距离它d[i]之内的某个点上必须有消防站.求符合上述条件的最小费用建站方 ...

  9. POJ 2342 (树形DP)

    题目链接: http://poj.org/problem?id=2342 题目大意:直属上司和下属出席聚会.下属的上司出现了,下属就不能参加,反之下属参加.注意上司只是指直属的上司.每个人出席的人都有 ...

最新文章

  1. linux刷新挂载信息,Linux系统下如何挂载FAT32的U盘
  2. FPGA的历史、现状和未来
  3. C++ explicit关键字详解(转载)
  4. 用友服务器文件如何查找,如何查询用友t3服务器地址
  5. python基本语法语句-python学习笔记:基本语法
  6. 基于特征点匹配的自适应目标跟踪算法
  7. 随机抽样java_java生成抽样随机数的多种算法
  8. mysql帐号不允许从远程登陆
  9. Oracle分页查询格式(八)
  10. php做网页的流畅,Easying轻量流畅
  11. 又一轮子?Typescript+React+Redux,放弃saga,支持服务器渲染同构
  12. 凸优化第九章无约束优化 9.1无约束优化问题
  13. sip android 客户端,为什么星号无法与android sip客户端正常工作?
  14. [教程][6月4日更新]VMware 8.02虚拟机安装MAC lion 10.7.3教程 附送原版提取镜像InstallESD.iso!...
  15. 虚拟机下搭建一个dns服务器,虚拟机下DNS服务器配置.doc
  16. 测试方法论——数据驱动测试
  17. CSS控制多行文本省略--显示省略号
  18. leetcode剑指offe刷题-第一题-用两个栈实现队列
  19. 如何一键重装Win10系统图文教程
  20. MVC学习七:Razor布局之加载分部视图【PartialView】

热门文章

  1. Fiddler之为什么我没有抓到网络请求的js链接
  2. 浙大计算机系可以转专业吗,求教。。浙大转专业容易吗
  3. java idea 模块_idea 多模块项目依赖父工程class找不到问题的方法
  4. 如何判断对象是不是出轨了?
  5. 学霸真的比学渣更讨女孩子欢心吗?
  6. 有图有真相!这世界上,竟有人跟你长得一模一样!
  7. java 继承与多态 习题_JAVA基础 第4章继承与多态_练习题_200910
  8. 爱卡创誓记java刷钱_【178创誓记】快速升级:40到50级只需要两天的黄金刷
  9. java junit4_JUnit4使用Java5中的单元测试
  10. java富文本如何转义_富文本编辑器wangEditor中转义字符的问题