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
北京大学《ACM/ICPC大学生程序设计竞赛训练》暑期课面向全球招生!

这样做怎么就错了呢?我好郁闷!求解脱!

Posted by chenxuan123456789 at 2012-08-19 15:10:47 on Problem 1330
#include <stdio.h>
#include <string.h>
#define size 10010
int sets[size];
int flag[size];
int n;
void make_set()
{
    int i;
    for(i=1;i<=n;i++)
    sets[i]=i;
}
int find_set(int x)
{
    int y=sets[x];
    flag[x]=1;
    if(x==y)
    return y;
    else
    {
        y=find_set(y);
        sets[x]=y;
        return y;
    }
}
int result(int n)
{
    if(flag[n])
    return n;
    else
    return result(sets[n]);
}
int main()
{
    int cases,i,father,son,x,y;
    scanf("%d",&cases);
    while(cases--)
    {
        scanf("%d",&n);
        make_set();
        for(i=1;i<n;i++)
        {

            scanf("%d %d",&father,&son);
            sets[son]=father;
        }
		//for(i=1;i<=n;i++)
		//printf("%d\n",sets[i]);
        memset(flag,0,sizeof(flag));
        scanf("%d %d",&x,&y);
		//printf("%d %d\n",x,y);
        find_set(x);
		//for(i=1;i<=n;i++)
		//printf("%d\n",sets[i]);
        printf("%d\n",result(y));
    }
    return 1;
}

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