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:为什么我写的并查集就不对呢? 牛哥帮小弟看看,小弟第一次发帖, 感谢!

Posted by mayunfeng at 2009-04-07 20:34:03 on Problem 2492
In Reply To:为什么我写的并查集就不对呢? 牛哥帮小弟看看,小弟第一次发帖, 感谢! Posted by:mayunfeng at 2009-04-07 20:33:01
> #include<stdio.h>
> #include<algorithm>
> #include<stdlib.h>
> #include<string.h>
> int num[1000001];
> int parent[100001];
> void chushihua(int n)
> {
>    int i ;
>    for(i = 0 ; i < n; i++){
>      parent[i] =  i;
>      num[i] = 1;     
>      }
>      
> }
> 
> void hebin(int x , int y)
> {
>     if(num[x] >= num[y]){
>         num[x]+=num[y]; 
>         parent[y] = x;
>         }
>     else{
>         num[y]+=num[x];
>         parent[x] = y;
>         }      
> }
> 
> int find(int t)
> {
>      if(t!=parent[t])
>      parent[t] = find(parent[t]);
>      return parent[t];   
>          
>     
> }
> int main()
> {
>     int count ;
>     int n  , m ,i ,x, y ,xx, yy , flag;
>     int ans = 1;
>      scanf("%d",&count);
>      while(count --)
>      {  
>          flag =  1;
>          scanf("%d%d",&n,&m);
>          chushihua(n);
>          for(i = 0 ; i < m ; i++)
>          {
>            scanf("%d%d",&xx,&yy);
>              x = find(xx);
>              y = find(yy);
>              
>              if(x == y)
>              flag = 0;
>              hebin(x, y);
>          }         
>      printf("Scenario #%d:\n",ans++);
>           if(flag)
>             printf("No suspicious bugs found!\n");
>             else
>             printf("Suspicious bugs found!\n"); 
>           if(count)  printf("\n");        
>      } 
>       
>      system("pause");
>      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