#include<iostream>
#include<cstring>
using namespace std;
int a[];
int n,m,l,ci,di; int root(int x) //找到根节点
{
int r = x;
while(r != a[r])
r = a[r];
int i = x,j;
while(i != r) //压缩路径
{
j = a[i];
a[i] = r;
i = j;
}
return r;
} void mix(int x,int y)   //混合
{
int fx = root(x),fy = root(y);
if(fx != fy)
a[fy] = fx; //根节点的值覆盖
} int main()
{
int i,j,t1,t2;
// memset(a,0,sizeof(a)); //过程中超时了,除去这一条就AC了
cin>>n>>m;
for(i = ;i <= n;i++)
a[i] = i;
for(i = ;i <= m;i++)
{
cin>>ci>>di;
mix(ci,di);
}
cin>>l;
for(i = ;i <= l;i++)
{
cin>>t1>>t2;
if(root(t1) != root(t2))
cout<<"They aren't relative!"<<endl;
else
cout<<"They are relative!"<<endl;
} return ;
}

这是自学的并查集,然后在oj上敲的题。有关并查集的学习可以百度。

                                                                           -----15:44:28 2017-06-10

并查集练习(0743) SWUST OJ的更多相关文章

  1. [Swust OJ 772]--Friend(并查集+map的运用)

    题目链接:http://acm.swust.edu.cn/problem/772/ Time limit(ms): 1000 Memory limit(kb): 65535    Descriptio ...

  2. [Swust OJ 1091]--土豪我们做朋友吧(并查集,最值维护)

    题目链接:http://acm.swust.edu.cn/problem/1091/ Time limit(ms): 1000 Memory limit(kb): 32768   人都有缺钱的时候,缺 ...

  3. [Swust OJ 856]--Huge Tree(并查集)

    题目链接:http://acm.swust.edu.cn/problem/856/ Time limit(ms): 1000 Memory limit(kb): 10000 Description T ...

  4. 安科 OJ 1190 连接电脑 (并查集)

    时间限制:1 s 空间限制:128 M 传送门:https://oj.ahstu.cc/JudgeOnline/problem.php?id=1190 题目描述 机房里有若干台电脑,其中有一些电脑已经 ...

  5. 杭电OJ——1198 Farm Irrigation (并查集)

    畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...

  6. 洛谷OJ P1196 银河英雄传说(带权并查集)

    题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山 ...

  7. 九度OJ 1446 Head of a Gang -- 并查集

    题目地址:http://ac.jobdu.com/problem.php?pid=1446 题目描述: One way that the police finds the head of a gang ...

  8. 九度OJ 朋友圈 -- 并查集

    题目地址:http://ac.jobdu.com/problem.php?pid=1526 题目描述: 假如已知有n个人和m对好友关系(存于数字r).如果两个人是直接或间接的好友(好友的好友的好友.. ...

  9. ZOJ 3521 Fairy Wars oj错误题目,计算几何,尺取法,排序二叉树,并查集 难度:2

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3521 ATTENTION:如果用long long 减小误差,这道题只能用 ...

随机推荐

  1. WinRAR去广告

    许多解压软件的广告看着令人头疼,今天我就给大家分享一个把WinRAR软件的广告去掉的方法. 环境:     win rar     restorator 2007(腾讯软件直接下载即可) 步骤: 首先 ...

  2. The Multilinear Structure of ReLU Networks

    两种非常常见的非线性单元:rectified linear units (ReLUs) 和 leaky ReLUs 我们选取binary hinge loss进行二分类 对于多分类,我们可以定义mul ...

  3. [转帖]微軟将从 .NET 4 以后的版本弃用 System.Data.OracleClient

    转帖--微軟将从 .NET 4 以后的版本弃用 System.Data.OracleClient Posted on -- : eaglet 阅读() 评论() 编辑 收藏 原贴 http://www ...

  4. Protobuffer教程

    目录 什么是protobuffer? protobuffer是如何工作的? 为什么不用xml? 1.什么是protobuffer? protobuffer是一种灵活,高效,自动化的机制,用于序列化结构 ...

  5. CH5104 I-country[线性DP+分类讨论]

    http://contest-hunter.org:83/contest/0x50%E3%80%8C%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%E3%80%8D%E4%B ...

  6. Elasticsearch在Centos 7上的安装与配置

    https://segmentfault.com/a/1190000011899522 https://blog.csdn.net/xxxxxx91116/article/details/171362 ...

  7. shell脚本日志指南(1)

    1.初始化日志文件.对文件的大小进行判断和压缩 LOG=$ if [ -e "${LOG}" ];then LOG_SIZE=`ls -l "${LOG}"| ...

  8. Acwing-120-防线(二分,前缀和)

    链接: https://www.acwing.com/problem/content/122/ 题意: 达达学习数学竞赛的时候受尽了同仁们的鄙视,终于有一天......受尽屈辱的达达黑化成为了黑暗英雄 ...

  9. 编程语言类别;运行Python程序的方式;变量和常量;Python程序的垃圾回收机制;

    目录 编程语言分类 运行Python程序的两种方式 1.交互式 变量与常量 1.变量 2.常量 3.小整数池 垃圾回收机制 编程语言分类 编程语言分为: 1.机器语言:直接用二进制的0和1和计算机(C ...

  10. mysql之单表条件查询

    create table staff_info( id int primary key auto_increment, name varchar(32) not null, age int(3) un ...