Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
以下代码G++ AC,C++ CE#include <cstdio> #include <string.h> #include <algorithm> using namespace std; const int NMax=20100; struct edge { int num; edge *next; }*A[NMax],pool[NMax*2]; int N,L,C[NMax]; bool visit[NMax]; void Build(int x,int y) { edge *p=&pool[L++],*q=&pool[L++]; p->num=y;p->next=A[x];A[x]=p; q->num=x;q->next=A[y];A[y]=q; } int ret1,ret2; void DFS(int a) { visit[a]=1; C[a]=1; int Max=0; for(edge *p=A[a];p;p=p->next) if(!visit[p->num]){ DFS(p->num); Max=max(C[p->num],Max); C[a]+=C[p->num]; } Max=max(N-C[a],Max); if(Max<ret2) {ret1=a;ret2=Max;} } int main() { int T,x,y; scanf("%d",&T); while(T--) { L=0;ret2=100000000; memset(A,0,sizeof(A)); memset(visit,0,sizeof(visit)); memset(C,0,sizeof(C)); scanf("%d",&N); for(int i=1;i<N;i++) { scanf("%d%d",&x,&y); Build(x,y); } DFS(1); printf("%d %d\n",ret1,ret2); } getchar();getchar(); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator