The attribute value of a vertex is: the maximum number of nodes of the connected component obtained after removing the vertex and all the edges connected to the vertex.
The center of gravity of the tree is defined as: a vertex. The attribute value of this vertex is the smallest among all vertices.
Sgu 134 identifies all centers of gravity and the attribute values of the center of gravity.
Consider using a tree-like DP.
DP [u] indicates the maximum number of nodes in the connected branch after the U point is cut.
TOT [u] records the total number of nodes (including the root) of the subtree rooted in U ).
Then, the two Arrays can be preprocessed with a DFS. Then enumerate each vertex. The attribute value of each vertex is actually max (DP [u], n-tot [u]), because it is possible that the largest connected branch is at or above U's father. Then record the answer.
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#include <vector>using namespace std;vector<int> G[16005];int dp[16005],tot[16005];vector<int> ans;void dfs(int u){ tot[u] = 1; for(int i=0;i<G[u].size();i++) { int v = G[u][i]; if(tot[v] == -1) dfs(v); else continue; dp[u] = max(dp[u],tot[v]); tot[u] += tot[v]; }}int main(){ int n,i,j,u,v; scanf("%d",&n); for(i=0;i<=n;i++) G[i].clear(); for(i=0;i<n-1;i++) { scanf("%d%d",&u,&v); G[u].push_back(v); G[v].push_back(u); } memset(dp,0,sizeof(dp)); memset(tot,-1,sizeof(tot)); dfs(1); int mini = Mod; for(i=1;i<=n;i++) { int tmp = max(dp[i],n-tot[i]); if(tmp < mini) { ans.clear(); ans.push_back(i); mini = tmp; } else if(tmp == mini) ans.push_back(i); } printf("%d %d\n",mini,ans.size()); sort(ans.begin(),ans.end()); printf("%d",ans[0]); for(i=1;i<ans.size();i++) printf(" %d",ans[i]); puts(""); return 0;}
View code