The main topic: a Tree of n nodes, find the smallest node of the largest subtree.
Problem Analysis: The process is similar to the center of gravity.
The code is as follows:
# include<iostream># include<cstdio># include<cstring># include<vector># include<queue ># include<list># include<set># include<map># include<string># include<cmath># include<cstdlib># include<algorithm>using namespace std;# define LL long longconst int n=1005;const int INF=1 000000000;int n,dp[n*20];int size[n*20];vector<int>e[n*20];void init () {scanf ("%d", &n); for (int i=1;i<= N;++i) e[i].clear (); int a,b;for (int i=1;i<n;++i) {scanf ("%d%d", &a,&b); E[a].push_back (b); E[b].push_back ( a);}} void Dfs (int u,int fa) {size[u]=1;for (int i=0;i<e[u].size (); ++i) {int v=e[u][i];if (V==FA) Continue;dfs (v,u); Size[u] +=SIZE[V];}} void dfs1 (int u,int fa) {dp[u]=n-size[u];for (int i=0;i<e[u].size (); ++i) {int v=e[u][i];if (V==FA) Continue;dp[u]=max (Dp[u],size[v]);d FS1 (V,u);}} void Solve () {DFS (1,-1);d FS1 (1,-1);DP [0]=inf;int root=0;for (int i=1;i<=n;++i) if (Dp[i]<dp[root]) root=i;printf ("%d%d\n", Root,dp[root]);} int main () {int t;scanf ("%d", &t), while (t--) {init (); Solve ();} return 0;}
POJ-1655 Balancing Act