Seek the center of gravity of the tree, directly as a template bar. First look at the POJ topic to know the center of gravity what meaning ...
Center of Gravity: Minimum number of nodes for maximum connected blocks after deleting this node
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <queue>5#include <stack>6 using namespacestd;7 #defineLL Long Long8 #defineCLC (A, B) memset (A,b,sizeof (a))9 #defineINF 0x3f3f3f3fTen Const intmaxn=20010; Onevector<int>V[MAXN]; A intVIS[MAXN],DP[MAXN],SON[MAXN]; - intAns,sizee,n; - voidDfsintx) { the intBal=0; -son[x]=0; -vis[x]=true; - for(intI=0;i< (int) v[x].size (); i++){ + intto=V[x][i]; - if(Vis[to])Continue; + Dfs (to); Ason[x]+=son[to]+1; atBal=max (bal,son[to]+1); - } -Bal=max (bal,n-son[x]-1); - if(bal<sizee| | (Bal==sizee && x<ans)) { -Ans=x,sizee=Bal; - } in } - intMain () { to intT; +scanf"%d",&T); - while(t--){ thescanf"%d",&n); * for(intI=1; i<=n;i++) $ v[i].clear ();Panax Notoginseng for(intI=1; i<=n-1; i++){ - intx, y; thescanf"%d%d",&x,&y); + v[x].push_back (y); A v[y].push_back (x); the } +CLC (VIS,0); -Sizee=inf; $Dfs1); $printf"%d%d\n", Ans,sizee); - } - return 0; the}View Code
POJ 1655 Balancing Act (center of gravity of the tree)