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
Output
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
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 }
原文:http://www.cnblogs.com/zgglj-com/p/7748532.html