Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

以下代码G++ AC,C++ CE

Posted by 5D6D2 at 2012-09-27 18:40:08 on Problem 1655
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator