POJ 1703 Find them, Catch them 并查集的应用
题意:城市中有两个帮派,输入中有情报和询问。情报会告知哪两个人是对立帮派中的人。询问会问具体某两个人的关系。
思路:并查集的应用。首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个集合中,如果是,则表明两个人的关系已知。
本题还需要判断出两人是不是在同一帮派,这里用一个relation数组来维护每个人与根结点的关系。
0表示该人与根节点为同一个帮派。
1表示该人与根节点为对立帮派。
并查集有两个基本操作:find——查找根节点, merge——合并两个集合。
在合并两个集合时,其中一个集合的relation的值要发生变化。在这里只需要改变集合根节点的relation(改变的规则见代码中merge函数,relation改变的规则自己举几个例子就明白了)。
其余点的值在find函数压缩路径时就顺便完成了。relation的状态只有两种(0和1),在压缩路径时的变化规则就是点到根节点的relation=(与父亲结点的relation+父亲结点与根节点的relation)% 2。
#include<stdio.h>
#define maxn 100010
int father[maxn];
bool relation[maxn];
int Find(int x)
{
if (father[x] != x)
{
int t = father[x];
father[x] = Find(father[x]);
relation[x] = (relation[t] + relation[x]) % ;
}
return father[x];
}
void Merge(int x,int y)
{
int a = Find(x);
int b = Find(y);
father[a] = b;
if (relation[y] == )
relation[a] = ^ relation[x];
else relation[a] = relation[x];
}
int main()
{
int t, n, m;
scanf("%d",&t);
while (t--)
{
scanf("%d%d",&n,&m);
for (int i = ; i <= n; i++)
{
father[i] = i;
relation[i] = ;
}
char op;
int a, b;
while (m--)
{
getchar();
scanf("%c",&op);
if (op == 'D')
{
scanf("%d%d",&a,&b);
Merge(a, b);
}
else
{
scanf("%d%d",&a,&b);
if (Find(a) == Find(b))
{
if (relation[a] == relation[b])
printf("In the same gang.\n");
else
printf("In different gangs.\n");
}
else
printf("Not sure yet.\n");
}
}
}
return ;
}
POJ 1703 Find them, Catch them 并查集的应用的更多相关文章
- POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集
POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...
- poj.1703.Find them, Catch them(并查集)
Find them, Catch them Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
- POJ 1703 Find them, catch them (并查集)
题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2 D 3 4 D 5 6...这就至少有3个集合了.并且 ...
- POJ 1703 Find them, Catch them(并查集高级应用)
手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...
- POJ 1703 Find them, Catch them 并查集,还是有点不理解
题目不难理解,A判断2人是否属于同一帮派,D确认两人属于不同帮派.于是需要一个数组r[]来判断父亲节点和子节点的关系.具体思路可参考http://blog.csdn.net/freezhanacmor ...
- [并查集] POJ 1703 Find them, Catch them
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43132 Accepted: ...
- POJ 1703 Find them, Catch them(种类并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41463 Accepted: ...
- hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them
http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...
- POJ 1703 Find them, Catch them (数据结构-并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31102 Accepted: ...
随机推荐
- git 远程仓库 与本地项目 挂钩 全过程
摘要:学了Android 快三个月了,依旧不会git,真的有些丢人.git是一个非常棒的团队协作的工具.其实也是分分钟的事情.Follow Me! Step 1 在码云上新建一个项目,作为远程仓库.里 ...
- 使用dnspod遭遇的奇特问题以及背后的原因与临时解决方法
由于园子里有不少用户在使用dnspod,我们觉得有必要将这两天blogjava.net域名在dsnpod遇到的奇特问题分享一下,以免再有人踩着这个坑. 12月11日,我们登录到dnspod的后台时,大 ...
- Careercup - Microsoft面试题 - 5188169901277184
2014-05-12 06:12 题目链接 原题: Write a function to retrieve the number of a occurrences of a substring(ev ...
- Oracle 学习笔记(Windows 环境下安装 + PL/SQL)
Oracle 安装.PL/SQL 配置使用 前言:因更换机械硬盘为 SSD 固态硬盘装了新 Windows 7 系统,需要重新搭建开发环境,把 Oracle 安装过程和 PL/SQL 配置使用做下笔 ...
- mongo命令
安装mongo http://docs.mongodb.org/master/tutorial/install-mongodb-on-redhat-centos-or-fedora-linux/ 启动 ...
- Lambda表达式的本质
//.net 1.0写法 /*delegate bool MyMethod(string s); bool myMethod(string s) { return s.IndexOf("ab ...
- java基础-容器
已经写了一段时间JAVA代码了,但仔细想来,却发现对JAVA的很多方面还是一片迷茫. 利用周末补一下基础知识吧. 大致列一下这个周末需要学习的内容 1 容器 (本节内容) 2 线程 3 流 目录 1 ...
- ruby linux连接windows执行dos命令
在整个开发流程中,gitlab-runner-ci是搭建在linux下.web自动化是在windows下面进行的. 如果ci构建完版本.部署完后,需要触发启动自动化执行. 那么我们需要在部署完后在li ...
- [UOJ#132][BZOJ4200][luogu_P2304][NOI2015]小园丁与老司机
[UOJ#132][BZOJ4200][luogu_P2304][NOI2015]小园丁与老司机 试题描述 小园丁 Mr. S 负责看管一片田野,田野可以看作一个二维平面.田野上有 \(n\) 棵许愿 ...
- css中按钮的四种状态
css中按钮有四种状态 1. 普通状态2. hover 鼠标悬停状态3. active 点击状态4. focus 取得焦点状态 .btn:focus{outline:0;} 可以去除按钮或a标签点击后 ...