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

这段 344MS

Posted by speedcell4 at 2011-08-01 16:47:50 on Problem 2524
In Reply To:两段并查集代码,谁能告诉我为什么第二段要慢些么…… Posted by:speedcell4 at 2011-08-01 16:46:43
#include<iostream>
#define SIZE 50001
using namespace std;
int n,m,x,y,times=1,result=0;
int a[SIZE]={0};
void Init(int n)
{
    result=n;
    for(int i=0;n-i>0;i++) a[i]=i;
}
int findPa(int n)
{
    if(a[n]!=n) return a[n]=findPa(a[n]);
    else return n;
}
void Union(int x,int y)
{
    int xx=findPa(x);
    int yy=findPa(y);
    if(xx==yy) return;
    else
    {
        a[xx]=yy;
        result--;
        return;
    }
}
int main()
{
    while(scanf("%d %d",&n,&m),n||m)
    {
        Init(n);
        for(int i=0;m-i>0;i++)
        {
            scanf("%d %d",&x,&y);
            Union(x,y);
        }
        printf("Case %d: %d\n",times++,result);
    }
    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