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

牛人给看下哈……先谢了……(runtime error)

Posted by nuaa_040930425 at 2010-07-26 21:28:41 on Problem 1988
#include<stdio.h>
int d[30005],f[30005],s[30005],n;
void merge(int a,int b)
{
     int x1=a,x2=a,y=b;
     //分别记录  f(father) s(son)
     while (y!=f[y]) y=f[y];
     while (x1!=f[x1]) { x1=f[x1]; d[x1]+=(d[y]+1); }
     while (x2!=s[x2]) { x2=s[x2]; d[x2]+=(d[y]+1); }
     d[a]+=(d[y]+1);
     s[x2]=y;
     f[y]=x2;
}
int main()
{
    int i,a,b;
    char ch;
    freopen("aoe.in","r",stdin);
    freopen("aoe.out","w",stdout);
    scanf("%d",&n);
    for (i=1;i<=n;i++)
        {
                      f[i]=i;
                      s[i]=i;
                      d[i]=0;
        }
     for (i=0;i<n;i++)
    {
        scanf("%c",&ch);
        scanf("%c",&ch);  
        if (ch=='M')
        {
                    scanf("%d %d",&a,&b);
                    merge(a,b);
        }
        if (ch=='C')
        {
                    scanf("%d",&a);
                    printf("%d\n",d[a]);
        }
    }
    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