POJ 1703 Find them, Catch them(并查集高级应用)
手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893
URL: http://poj.org/problem?id=1703
题目大意:本题即很经典的“龙帮虎帮”问题。
有n个元素(n<=1e5),分布在两个不同的集合里。
现在有M个语句(m<=1e5),每个语句共两种:(1) 给定某两个元素在不同的集合中。(2) 询问两个元素是否在同一集合中。对于是、否、无法确定的情况,分别输出"In the same gang.", "In different gangs.", "Not sure yet."
思路分析:假如给定的是两个元素在同一集合中,思路就很明确了。
但是现在给定的是两个元素不同集合,而且已知仅有两个集合,于是我们就可以想办法将其转化为两元素同集合问题。
设有元素A,B, 则我们将每个元素分别“克隆”为A', B', 但将她们放入分别与本身不同的另外一个集合中。
同时,保证A和A', B和B', ..., 永远不会同集合。
然后,假如已知A与B不同集合,则需将A和B', B和A'分别放入同一集合(union操作),假如已知A与B同集合,则需把A和B, A'和B'进行union一下即可。
最后,回答询问时,有以下三种情况:
(1) A与B同集合,或A'与B‘同集合:In the same gang.
(2) A与B'同集合,或B与A'同集合:In different gangs.
(3) 其他:Not sure yet.
代码呈现:(Time:532MS ,Memory:960K ,Code:1005B)
#include<cstdio>
using namespace std;
const int MAXN = 1e5;
int fa[MAXN*2+2];
int n,m;
int find_fa(int u)
{
int i,j,k;
i = u;
while(fa[i] != i)
{
i = fa[i];
}
fa[u] = i;
j = u;
while(j != i)
{
k = fa[j];
fa[j] = i;
j = k;
}
return i;
}
int main()
{
int i,t,p,q,pp,qq,x,y;
char ch[3];
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=1; i<=2*n; i++) //Union-Find Sets init
{
fa[i] = i;
}
for(i=1; i<=m; i++) //calculate while reading
{
scanf("%s%d%d",ch,&x,&y);
if(ch[0] == 'A')
{
p = find_fa(x);
q = find_fa(y);
pp = find_fa(x+n);
qq = find_fa(y+n);
if(p == q) printf("In the same gang.\n");
else if(pp == q || p == qq) printf("In different gangs.\n");
else printf("Not sure yet.\n");
}
else if(ch[0] == 'D')
{
p = find_fa(x);
q = find_fa(y);
pp = find_fa(x+n);
qq = find_fa(y+n);
if(pp != q) fa[pp] = q;
if(qq != p) fa[qq] = p;
}
}
}
return 0;
}
类似题目:poj 2492 A Bug's Life (几乎一模一样)
poj 1182 && luogu P2024 [NOI 2001]食物链 (一样的思路,元素分成三类)
luogu P1525 [NOIP 2010提高组] 关押罪犯 (个人认为难度较大)
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 并查集的应用
题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...
- 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: ...
随机推荐
- Android 重写onBackPressed()方法 遇到的问题
1.resultCode的值一直为0 问题描述:AActivity调用startActivityForResult()方法,启动BActivity,然后在BActivity的onBackPressed ...
- 摄像头ov2685中关于sensor id 设置的相关的寄存器地址【转】
本文转载自:http://blog.csdn.net/morixinguan/article/details/51220992 OV2685 : CHIP_ID address : 0x300A ...
- 【BZOJ 3790】 神奇项链
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3790 [算法] manacher + 贪心 [代码] #include<bit ...
- the user must supply a jdbc connection 错误解决方法
转自:https://blog.csdn.net/actionzh/article/details/54200451 今天在配置hibernate之后,进行添加数据测试时,运行中报出了 the use ...
- 快速排序及三向切分快排——java实现
快速排序也是一种分治算法.主要思想是选取一个切分点,将大于切分点的元素都放置到数组右侧,小于切分点的元素都放置到数组左侧:然后递归,再对切分点左侧和右侧分别排序. 归并排序时递归在前,归并在后,快速排 ...
- [Apple开发者帐户帮助]四、管理密钥(2)获取密钥标识符
创建JSON Web令牌(JWT)以与启用的服务进行通信时,需要密钥标识符. 获取密钥标识符 在“ 证书,标识符和配置文件”中,选择侧栏中“键”下的“全部”. 在右侧,选择私钥. 密钥标识符显示在密钥 ...
- 基于CGAL的Delaunay三角网应用
目录 1. 背景 1.1 CGAL 1.2 cgal-bindings(Python包) 1.3 vtk-python 1.4 PyQt5 2. 功能设计 2.1 基本目标 2.2 待实现目标 3. ...
- python2.X现在不能安装Django了:Collecting django Using cached Django-2.0.tar.gz
使用pip安装django2: pip install django 报错: Collecting django Using cached Django-2.0.tar.gz Complete ...
- Android Studio项目中有用文件与可忽略文件(初学者)
可通过Settings --> Version Control --> Ignored Files进行设置或察看: 支持指定文件或文件夹,也支持匹配模式. Android Studio 中 ...
- 用List表示多重性
练习目标-在类中使用List作为模拟集合操作: 在本练习中,将用List实现银行与客户间的多重关系. 任务 对银行来说,可添加Bank类. Bank 对象跟踪自身与其客户间的关系.用Customer对 ...