POJ 3107 Godfather (center of gravity of the tree)
Or the center of gravity of the naked tree, but this requirement outputs all the centers of gravity. Very simple.
The Code is as follows:
#include
#include
#include
#include
#include #include
#include
#include
#include
#include
using namespace std;#define LL __int64#define pi acos(-1.0)//#pragma comment(linker, /STACK:1024000000)#define root 1, n, 1#define lson l, mid, rt<<1#define rson mid+1, r, rt<<1|1const int mod=1e9+7;const int INF=0x3f3f3f3f;const double eqs=1e-9;const int MAXN=50000+10;struct node { int v, next;} edge[MAXN<<1];int head[MAXN], cnt, min1, n, num;int sum[MAXN], ans[MAXN];void add(int u, int v){ edge[cnt].v=v; edge[cnt].next=head[u]; head[u]=cnt++;}void init(){ memset(head,-1,sizeof(head)); cnt=0; min1=INF;}void dfs(int u, int fa){ int max1=0, i, tot=0; sum[u]=1; for(i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(v==fa) continue ; dfs(v,u); sum[u]+=sum[v]; max1=max(max1,sum[v]); tot+=sum[v]; } max1=max(max1,n-tot-1); if(min1>max1) { min1=max1; num=0; ans[num++]=u; } else if(min1==max1) { ans[num++]=u; }}int main(){ int u, v, i; while(scanf(%d,&n)!=EOF) { init(); for(i=1; i