设为首页 加入收藏

TOP

poj 2378 Tree Cutting 树形dp
2015-07-20 17:51:09 来源: 作者: 【 】 浏览:2
Tags:poj 2378 Tree Cutting 树形

点击打开链接题目链接

Tree Cutting
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3677 Accepted: 2183

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.

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

找树的重心

用邻接表写的


#include
  
   
#include
   
     #include
    
      using namespace std; const int MAXN=100005; struct Edge { int st,next; }edge[2*MAXN]; int ans[MAXN]; int head[MAXN]; int n; int s,e; bool vis[MAXN]; void add(int s,int t) { edge[e].st=s; edge[e].next=head[t]; head[t]=e++; } int dfs(int x) { int num=0,sum=0; int i; vis[x]=1; int flag=0; for(i=head[x];i!=-1;i=edge[i].next) { if(vis[edge[i].st]==0) { num=dfs(edge[i].st); sum+=num; if(num>n/2) flag=1; } } if(n-sum-1>n/2) flag=1; if(flag==0) ans[s++]=x; return sum+1; } int main() { int i,a,b; while(scanf("%d",&n)!=EOF) { memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis)); s=e=0; for(i=1;i
     
      

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇UVa488 - Triangle Wave 下一篇UVa 11354 Bond 最小生成树+LCA倍..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C++ 语言社区-CSDN社 (2025-12-24 17:48:24)
·CSDN问答专区社区-CS (2025-12-24 17:48:22)
·C++中`a = b = c`与` (2025-12-24 17:48:19)
·C语言结构体怎么直接 (2025-12-24 17:19:44)
·为什么指针作为c语言 (2025-12-24 17:19:41)