题干:

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/2.输出满足条件的点,如果有多个这样的点,则升序输出。

输入:第一个表示有n个结点,接下来n-1行每行两个值,表示这两个结点相连接。

解题报告:

树形dp。(跟树的重心挺像,类似的方法dp就行了)

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
vector<int> vv[MAX];
int dp[MAX],sum[MAX],n;
int dfs(int cur,int root) {dp[cur] = 1, sum[cur] = 1;int up = vv[cur].size();for(int i = 0; i<up; i++) {int v = vv[cur][i];if(v == root) continue;int tmp = dfs(v,cur);sum[cur] += tmp;dp[cur] = max(dp[cur],tmp);}dp[cur] = max(dp[cur],n - sum[cur]);return sum[cur];
}
int main()
{while(~scanf("%d",&n)) {for(int i = 1; i<=n; i++) vv[i].clear(),dp[i]=1;for(int i = 1; i<=n-1; i++) {int x,y;scanf("%d%d",&x,&y);vv[x].pb(y);vv[y].pb(x);}dfs(1,-1);int flag = 0;for(int i = 1; i<=n; i++) {if(dp[i]*2 <= n) {flag = 1;printf("%d\n",i);}}if(flag == 0) puts("NONE");}return 0 ; }
//14:34 - 14:40

【POJ - 2378】Tree Cutting(树形dp,树的重心变形)相关推荐

  1. POJ - 4045 Power Station(树形dp/树的重心)

    题目链接:点击查看 题目大意:给出一个n个节点的树,我们需要选出一个节点,到其余任何节点的距离和最小 题目分析:这个题我的第一反应是用树的重心,先求出来符合条件的点,然后再跑一遍dfs求距离,最后输出 ...

  2. 树形dp树的重心(D - Godfather POJ - 3107)

    题目链接:https://cn.vjudge.net/contest/277955#problem/D 题目大意:求树的重心(树的重心指的是树上的某一个点,删掉之后形成的多棵树中节点数最大值最小). ...

  3. 树形dp——树的重心(2) 代码调试理解

    和树的最大独立问题类似,先任选一个结点作为根节点,把无根树变成有根树,然后设d(i)表示以i为根的子树的结点的个数.不难发现d(i)=∑d(j)+1,j∈s(i).s(i)为i结点的所有儿子结点的编号 ...

  4. HDU.5909.Tree Cutting(树形DP FWT/点分治)

    题目链接 \(Description\) 给定一棵树,每个点有权值,在\([0,m-1]\)之间.求异或和为\(0,1,...,m-1\)的非空连通块各有多少个. \(n\leq 1000,m\leq ...

  5. 树形dp ——树的重心

    1.只需要求出最大子树中节点数最小的数目即可 题意:有一个国王要把他的领土分给两个儿子,国王的领土是一棵树,N个结点,N-1条边把这些结点连起来,现在大小儿子要选择一个点作为他的首都,那么除首都分别是 ...

  6. POJ 3107 Godfather(树形DP(找重心))

    任重而道远 Description Last years Chicago was full of gangster fights and strange murders. The chief of t ...

  7. 树形DP+树状数组 HDU 5877 Weak Pair

    1 //树形DP+树状数组 HDU 5877 Weak Pair 2 // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 3 // 这道题要 ...

  8. 【牛客 - 303K第十五届浙江大学宁波理工学院程序设计大赛(同步赛)】Technology Tree(树形dp,tricks)

    题干: 在星际争霸(StarCraft)中,有3个种族.对于任意一个种族,他们的建筑建造都是有一个顺序的.这个顺序正好是一个树形结构,我们称之为"科技树"(Technology t ...

  9. POJ 3585 Accumulation Degree 树形dp

    题目链接 Accumulation Degree Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5388   Accepte ...

最新文章

  1. win7 php mysql扩展名_Win7 iis php mysql 开发环境配置(详细)
  2. usessl mysql_mysql数据库连接useSSL=true
  3. JAVA socket编程 Datagram套接字 UDP协议(转)
  4. nginx_反向代理
  5. java输入行数打印菱形_JAVA题,输入行数,输入列数,输出一个菱形
  6. 在Ubuntu 14.04平台上利用Intel的GPU实现硬件加速--基于VAAPI
  7. c语言 中断 局部变量 not allocated,C语言(函数)调用过程(略译)
  8. OC-引用计数器,内存管理,野指针
  9. 63.Linux/Unix 系统编程手册(下) -- 其他备选的IO模型
  10. 谷歌 kaptcha 图片验证码
  11. 必须安装三星系列android系统智能手机usb驱动程序,三星N9109W Android 5.0 (GALAXY Note 4 电信4G)usb驱动下载安装教程...
  12. shopex4.8.5 php5.6,ShopEx(网上商店系统)
  13. JAVA接入微信刷脸支付分支付【V2、V3两种接入都有提供】
  14. i7 13650hx参数 酷睿i713650hx性能怎么样相当于什么水平
  15. 导航算法A*的简单实现
  16. gdc服务器硬盘修复,GDC硬盘检测、阵列新建和修复.pptx
  17. goldenboy机器人_急求阿西莫夫机器人,基地,帝国三大系列的书名及其简介
  18. 谈一谈Java中的深拷贝和浅拷贝
  19. PHP Web开发框架Laravel如何配置
  20. 点击元素,目标元素显示和隐藏。点击其他非指定区域,目标元素隐藏

热门文章

  1. 思想已经高过行动好多了
  2. Hihocoer 1336 - Matrix Sum 二维树状数组
  3. Java学习笔记2——常用类
  4. c语言在函数中传递指针,[求助]关于文件指针在函数中传递的问题
  5. python可以开多少线程_python多线程详解
  6. Tecplot如何提取某点数据并导出
  7. python去重且顺序不变_Python实现嵌套列表去重方法示例
  8. arm for asterisk1.8
  9. 网络编程模型综述 之 UNIX网络I/O模型
  10. 串口通讯编程一日通2(Overlapped IO模型)