Tree Cutting POJ - 2378

Posted 啦啦啦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tree Cutting POJ - 2378相关的知识,希望对你有一定的参考价值。

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).
数据比较水,求树的重心,固定套路
 1 #include<queue>
 2 #include<vector>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<iostream>
 6 #include<algorithm>
 7 using namespace std;
 8 
 9 const int maxn=1e4+5;
10 
11 struct node{
12     int to,next;
13 }e[4*maxn];
14 
15 int n,tot;
16 int dp[maxn],num[maxn],head[maxn];
17 
18 void Inite(){
19     tot=0;
20     memset(head,-1,sizeof(head));
21 } 
22 
23 void addedge(int u,int v){
24     e[tot].to=v;
25     e[tot].next=head[u];
26     head[u]=tot++;
27 }
28 
29 void DFS(int pa,int u){
30     num[u]=1,dp[u]=0;
31     for(int i=head[u];i!=-1;i=e[i].next){
32         int v=e[i].to;
33         if(pa==v) continue;
34         DFS(u,v);
35         dp[u]=max(dp[u],num[v]);
36         num[u]+=num[v];
37     } 
38     dp[u]=max(dp[u],n-num[u]);
39 }
40 
41 void Solve(){
42     bool flag=false;
43     for(int i=1;i<=n;i++){
44         if(dp[i]<=n/2){
45             flag=true;
46             printf("%d\n",i);
47         }
48     }
49     if(!flag) printf("NONE\n");
50 }
51 
52 int main()
53 {   Inite();
54     scanf("%d",&n);
55     for(int i=2;i<=n;i++){
56         int u,v;
57         scanf("%d%d",&u,&v);
58         addedge(u,v);
59         addedge(v,u);
60     }
61     DFS(0,1);
62     Solve();
63     
64     return 0;
65 }

 

以上是关于Tree Cutting POJ - 2378的主要内容,如果未能解决你的问题,请参考以下文章

poj 2378 Tree Cutting 树形dp

POJ 2378.Tree Cutting 树形dp

POJ 2378 Tree Cutting (DFS)

POJ 2378 Tree Cutting 子树统计

hdu5909-Tree Cutting(树形dp)

POJ2311 Cutting Game(博弈论)