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: ...
随机推荐
- cf975d Ghosts
ref #include <algorithm> #include <iostream> #include <cstdio> #include <map> ...
- 【Swap Nodes in Pairs】cpp
题目: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1-> ...
- 理解机器为什么可以学习(五)---Noise and Error
之前我们讨论了VC Dimension,最终得到结论,如果我们的hypetheset的VC Dimension是有限的,并且有足够的资料,演算法能够找到一个hypethesis,它的Ein很低的话,那 ...
- poj3009 Curling 2.0 (DFS按直线算步骤)
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14563 Accepted: 6080 Desc ...
- java实现图的深度优先遍历和广度优先遍
首先需要知道的是,图的深度优先遍历是一种类似于树的前序遍历方式,即选择一个入口节点,沿着这个节点一直遍历下去,直至所有节点都被访问完毕:如果说,图的深度优先遍历类似于树的前序遍历的话,那么图的广度优先 ...
- MySQL误操作后如何快速恢复数据?
摘要: 利用binlog闪回误操作数据. 基本上每个跟数据库打交道的程序员(当然也可能是你同事)都会碰一个问题,MySQL误操作后如何快速回滚?比如,delete一张表,忘加限制条件,整张表没了.假如 ...
- MAC抓包工具charles(青花瓷)
下载链接:http://pan.baidu.com/s/1pL6ClBX 配置教程:http://blog.csdn.net/jiangwei0910410003/article/details/41 ...
- [luogu1357] 花园 [dp+矩阵快速幂]
题面: 传送门 思路: 把P形花圃记录为0,C形记录为1,那么一段花圃就可以状态压缩成一个整数 那么,我们可以有这样的状压dp: dp[i][S]表示前i个花圃,最后m个的状态为S的情况 如果这是一条 ...
- 一两眼题(oneortwo)
一两眼题(oneortwo) 题目描述 给出n个整数,依次为a1,a2,...an.n<=50000. 你要进行K次操作,0 <= k < =1,414,213,562 每次操作你算 ...
- codevs 1081 线段树练习 2 区间更新 单点查询 无lazy
题目描述 Description 给你N个数,有两种操作 1:给区间[a,b]的所有数都增加X 2:询问第i个数是什么? 输入描述 Input Description 第一行一个正整数n,接下来n行n ...