心如止水
Je n'ai pas le temps
posts - 400,comments - 130,trackbacks - 0
考察并查集的基本操作。
以下是我的代码:
#include<stdio.h>
#define maxn 5007
long n,m,p,f[maxn];
long Getf(long x)
{
    
if(f[x]==x) return x;
    f[x]
=Getf(f[x]);
    
return f[x];
}
void Union(long x,long y)
{
    
long fx=Getf(x),fy=Getf(y);
    
if(fx!=fy)
      f[fx]
=fy;
}
int main()
{
    
long a,b;
    scanf(
"%ld%ld%ld",&n,&m,&p);
    
for(long i=1;i<=n;i++) f[i]=i;
    
for(long i=1;i<=m;i++)
    {
       scanf(
"%ld%ld",&a,&b);
       Union(a,b);
    }
    
for(long i=1;i<=p;i++)
    {
       scanf(
"%ld%ld",&a,&b);
       
if(Getf(a)==Getf(b))
         printf(
"Yes\n");
       
else printf("No\n");
    }
return 0;
}


posted on 2010-03-21 21:32 lee1r 阅读(107) 评论(0)  编辑 收藏 引用 所属分类: 题目分类:数据结构

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理