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

自己想了好久,终于AC了,一次dfs,不过200多ms。

Posted by 1100012760 at 2012-10-23 01:05:58 on Problem 1655
#include<cstdio>
using namespace std;
struct node
{
    node* next;
    int num;
    node(){next=NULL;}
}head[20001];
int Max(int a , int b)
{
    if(a<b) return b;
    return a;
}
int n , index[20001];
bool tag[20001];
int dfs(int v)
{
    tag[v] = false;
    node* tem = &head[v];
    int tem1 , ret = 1;
    bool c = false;
    while(tem->next)
    {
        tem = tem->next;
        if(tag[tem->num])
        {
            c = true;
            tem1 = dfs(tem->num);
            if(index[v]<tem1) index[v] = tem1;
            ret += tem1;
        }
    }
    if(!c)
    {
        index[v] = n - 1;
        return ret;
    }
    index[v] = Max(n - ret,index[v]);
    return ret;
}
int main()
{
    int t , i , x , y , ans , l;
    node* pp[20001];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        ans = 999999;
        for(i = 1 ; i <= n ; i++)
        {
            head[i].next = NULL;
            pp[i] = &head[i];
            tag[i] = true;
            index[i] = 0;
        }
        for(i = 1 ; i < n ; i++)
        {
            scanf("%d%d",&x,&y);
            pp[x]->next = new node;
            pp[x] = pp[x]->next;
            pp[x]->num = y;
            pp[y]->next = new node;
            pp[y] = pp[y]->next;
            pp[y]->num = x;
        }
        dfs(1);
        for(i = 1 ; i <= n ; i++)
        {
            if(ans>index[i])
            {
                ans = index[i];
                l = i;
            }
        }
        printf("%d %d\n",l,index[l]);
    }
    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