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

注意用long

Posted by lvweihua at 2017-08-12 14:51:17 on Problem 2524
#include<cstdio>
#include<cstring>
const int MAXN=50005;
int par[MAXN],N;
long M;
int Find(int x){
    int s;
    for(s=x;par[s]>=0;s=par[s]);
    while(s!=x){
        int tmp=par[x];
        par[x]=s;
        x=tmp;
    }
    return s;
}
void Union(int x,int y){
    int r1=Find(x),r2=Find(y);
    if(r1==r2)return;
    int sum=par[r1]+par[r2];
    if(par[r1]<par[r2]){
        par[r1]=sum;
        par[r2]=r1;
    }
    else{
        par[r2]=sum;
        par[r1]=r2;
    }
}
int main()
{
    int t=0;
    while(scanf("%d%ld",&N,&M)&&N&&M){
        memset(par,-1,sizeof(par));
        int i,j;
        for(long k=0;k<M;k++){
            scanf("%d%d",&i,&j);
            Union(i,j);
        }
        long ans=0;
        for(long k=1;k<=N;k++)
            if(par[k]<=0)
                ans++;
        printf("Case %d: %d\n",++t,ans);
    }
    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