Title Address: POJ 3107
or the center of gravity of the bare tree, but this requires that all the center of gravity be output. Very simple.
The code is as follows:
#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>#include <time.h>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 intMod=1e9+7;Const intinf=0x3f3f3f3f;Const Doubleeqs=1e-9;Const intmaxn=50000+Ten;structNode {intV, Next;} edge[maxn<<1];intHEAD[MAXN], CNT, MIN1, N, num;intSUM[MAXN], ANS[MAXN];voidAddintUintV) {edge[cnt].v=v; Edge[cnt].next=head[u]; head[u]=cnt++;}voidInit () {memset(head,-1,sizeof(head)); Cnt=0; Min1=inf;}voidDfsintUintFA) {intmax1=0, I, tot=0; sum[u]=1; for(I=head[u]; i!=-1; I=edge[i].next) {intV=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; }}intMain () {intU, V, I; while(scanf("%d", &n)!=eof) {init (); for(i=1; i<n; i++) {scanf("%d%d", &u,&v); Add (U,V); Add (V,u); } DFS (1,-1); Sort (ans,ans+num); for(i=0; i<num; i++) {printf("%d", Ans[i]);if(i!=num-1)printf(" ");Else puts(""); } }return 0;}
POJ 3107 Godfather (center of the tree)