树形DP______Tree Cutting( POJ 2378 )

来源:互联网 发布:松井玲奈毕业 知乎 编辑:程序博客网 时间:2024/06/05 12:38

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

101 22 33 44 56 77 88 99 103 8

Sample Output

38


题意:

给一颗n个结点的树,节点编号为1~n,问删除一个节点之后,让剩下的分支中节点数量最多的尽量少。
   可能有多种方案,按编号顺序输出。


分析:

对于i节点去掉之后会产生其子树,和整个树去掉i为根节点的树。先统计出所有节点为根节点的子树节点数,然后走一发dp即可求出来。


代码:

#include <stdio.h>#include <algorithm>#include <string.h>#include <vector>using namespace std;int to[100010],ft[50010],nt[100010],tot;int num[50010];int book[50010];int n,u,v;void addedge(int u,int v){    to[tot] = v;    nt[tot] = ft[u];    ft[u] = tot ++;}int dfs(int x,int pre){    num[x] = 1;    int Max = 0;    for(int i = ft[x] ; i != -1 ; i = nt[i])    {        int y = to[i];        if(y == pre)continue;        int step = dfs(y,x);        num[x] += step;        Max = max(Max,step);    }    Max = max(Max,n - num[x]);    book[x] = Max;    return num[x];}int main(){    memset(ft,-1,sizeof(ft));    scanf("%d",&n);    for(int i = 1 ; i < n ; i ++)    {        scanf("%d%d",&u,&v);        addedge(u,v);        addedge(v,u);    }    dfs(1,0);    bool k = 0;    int Min = 0x3f3f3f3f;    for(int i = 1 ; i <= n ; i ++)        Min = min(Min,book[i]);    for(int i = 1 ; i <= n ; i ++)        if(book[i] == Min)    {        if(k == 0)printf("%d",i),k = 1;        else printf(" %d",i);    }    printf("\n");    return 0;}



0 0
原创粉丝点击