博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
并查集~
阅读量:7175 次
发布时间:2019-06-29

本文共 480 字,大约阅读时间需要 1 分钟。

ExpandedBlockStart.gif
并查集
#include <stdio.h>
int f[
10000];
int find(
int n)
{
    
if (f[n]!=n) f[n]=find(f[n]);
    
return f[n];
}
int main()
{
    
int i,b,a,t,n,m;
    
while (~scanf(
"
%d%d%d
",&m,&n,&t))
    {
        
for (i=
1;i<=m;i++)
            f[i]=i;
        
for (i=
1;i<=n;i++)
        {
            scanf(
"
%d%d
",&a,&b);
            a=find(a);
            b=find(b);
            f[a]=b;
        }
        
for (i=
1;i<=t;i++)
        {
            scanf(
"
%d%d
",&a,&b);
            a=find(a);
            b=find(b);
            
if (a==b)
                printf(
"
YES\n
");
            
else 
                printf(
"
NO\n
");
        }
    }
    
return 
0;
}

转载于:https://www.cnblogs.com/modiz/archive/2013/04/18/3029265.html

你可能感兴趣的文章
1118. Birds in Forest (25)
查看>>
Introduction to jQuery Mobile
查看>>
Android Audio System 之一:AudioTrack如何与AudioFlinger交换音频数据(转)0
查看>>
BZOJ2141排队——树状数组套权值线段树(带修改的主席树)
查看>>
Max Sequence
查看>>
第二篇*1、Python基本数据类型
查看>>
Mybatis中$和#的区别
查看>>
EntityFramework_基础
查看>>
maven常用命令介绍(持续更新)
查看>>
【题解】选牛
查看>>
css z-index
查看>>
php产品细节图多图上传示例代码 无刷新
查看>>
初始化mysql数据库提示缺少Data:dumper模块解决方法
查看>>
实时在线查询sql
查看>>
hadoop作业调优参数整理及原理(转)
查看>>
java自定义注解
查看>>
【原】web移动端常用知识点笔记
查看>>
c语言 贪食蛇小游戏
查看>>
Cloudera Impala 之 ORDER BY without LIMIT currently not supported
查看>>
心灵鸡汤----幸福在哪里?
查看>>