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

1611

Posted by 1363217105 at 2011-06-03 09:35:20 on Problem 1611
谁能帮我一下代码,怎么会超时啊
#include<iostream>
using namespace std;
int parent[30001],num[3001],root1,root2;
void set(int i)
{
 parent[i]=i;
 num[i]=1;
}
int find(int a)
{
    if(parent[a]==a)  return a;
    parent[a]=find(parent[a]);
    return parent[a];
}
void uni(int a,int b)
{
    root1=find(a);
    root2=find(b);
    if(root1==root2) return;
    if(num[root1]>=num[root2])
    {
        parent[root2]=root1;
        num[root1]+=num[root2];
    }
    else
    {
        parent[root1]=root2;
        num[root2]+=num[root1];
    }
}
int main()
{
    int first,next,i,j,m,n,k;
    while(cin>>m>>n)
    {
        if(m+n==0) break;
        for(i=0;i<m;i++)
        set(i);
        for(j=0;j<n;j++)
        {
            cin>>k;
            cin>>first;
            for(int z=1;z<k;z++)
            {
                cin>>next;
                uni(first,next);
            }
        }cout<<num[find(0)];
    }
    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