hoj2676 relation

/*This Code is Submitted by billforum for Problem 2676 at 2012-02-11 17:27:50*/
#include <stdio.h>  
#include <iostream>  
using namespace std;  
  
const int N=5000;
int root[N+5],rank[N+5];
void init()
{
        for(int i=1;i<=N;i++)
        {
                root[i]=i;
                rank[i]=1;
        }
        return;
}
int findroot(int x)
{
        if(root[x]==x) return x;
        else
        {
                //root[x]=findroot(root[x]);
                //return root[x];
                return root[x]=findroot(root[x]);
        }
}
void rel(int x,int y)
{
        int r1,r2;
        r1=findroot(x);
        r2=findroot(y);
        //root[r1]=r2;
        ///if(r1==r2) return;
        //else root[r1]=r2;
        
        
        if(r1==r2) return;
        else
          {
           if(rank[r1]==rank[r2]) 
                   {root[r1]=r2; 
                    rank[r2]++;
                   }
            else  if(rank[r1]<rank[r2]) root[r1]=r2;
          else  root[r2]=r1;
             
          }
         
        
        return;
}

bool qury(int x,int y)
{
        int r1=findroot(x);
        int r2=findroot(y);
        return (r1==r2);
}
int main(void)  
{  
   int n,m,Q;
   while(scanf("%d%d%d",&n,&m,&Q)==3)
   {
           int m1,m2;
           init();
           for(int i=1;i<=m;i++)
           {
                   //cin>>m1>>m2;
                   scanf("%d%d",&m1,&m2);
                   rel(m1,m2);
           }
           for(int j=1;j<=Q;j++)
           {
                   int q1,q2;
                  // cin>>q1>>q2;
                  scanf("%d%d",&q1,&q2);
                   if(qury(q1,q2)) //cout<<"yes"<<endl;
                   printf("yes\n");
                   else //cout<<"no"<<endl;
                   printf("no\n");
           }
           
           
   }//while
   
  
    return 0;  
} 

 

posted @ 2012-02-11 17:32  wuzhibin  阅读(142)  评论(0)    收藏  举报