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

Re:急需有人帮我看看1611并查集,为什么会超时啊

Posted by crazyyangdan at 2011-06-05 11:01:56 on Problem 1611
In Reply To:急需有人帮我看看1611并查集,为什么会超时啊 Posted by:1363217105 at 2011-06-03 16:05:40
> #include<iostream>
> using namespace std;
> int parent[30001],num[30001],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[root1]=root2;
>         num[root2]+=num[root1];
>     }
>     else
>     { //same
>         parent[root1]=root2;
>         num[root2]+=num[root1];
>     }
> }
> int main()
> {
>     int first,next,i,j,m,n,k;
>     while(1)
>     {   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