Balancing ACT Topic Links:
http://poj.org/problem?id=1655
Test instructions
Give a tree, seek the center of gravity of the tree and the number of nodes with the most nodes of the root node node, if there are more than the number of center of gravity output numbers smaller.
Exercises
The center of gravity of the tree: Find a node V, the root of V, so that V "node most sub-tree" node minimization
Water problem, casually with a node as the root of the deep search, where a node V "node most sub-tree" node number =min (sum[child[v]],n-∑sum[child[v]]-1)
Code
#include<stdio.h>Constint N=2e4+1;int cnt, head[N],ev[N*2],enext[N*2],res1, Res2;IntAbs(int x){return x>=0? x:-X;}voidAddedge(int U,Int V){Ev[CNT]=v; Enext[CNT]=head[u];head[u]=cnt++;}IntDfs(int ID,int FA,int n){int sum=0Mx=0;For(int I=head[ID];i!=-1; I=enext[I]){Int V=ev[I];If(V==fa)Continue;int W=Dfs(VIdN); sum+=w;If(W>mx) MX=w;}sum++;If(n-sum>mx) MX=n-sum;If(MX<res2|| (MX==res2&&id<res1)){Res2=mx; res1=id;}return sum;}voidClean(int n){CNT=0; res2=n+1;For(int I=1; I<=n; ++i) head[I]=-1;}voidSolve(){int TNXY;scanf("%d", &t);While(T--){scanf("%d", &n);If(n==1)While(1);Clean(n);For(int I=1; I<n; ++i){scanf("%d%d", &x, &y);Addedge(xY);Addedge(YX}dfs (1< Span class= "Sh_symbol" >,1,nprintf ( "%d%d\n< Span class= "sh_string" > ",res1,res2}}int main< Span class= "Sh_symbol" > () {solve (); Span class= "Sh_keyword" >return 0;}
POJ 1655:balancing Act