POJ_1703 Find them, Catch them 【并查集】
一、题面
二、分析
需要将并查集与矢量法则相结合。par数组用以记录父节点,rank用以记录与父节点的关系。如题意,有两种关系,设定0是属于同一个帮派,1表示不属于同一个帮派。
运用并查集的时候判断x,y时考虑几种情况:
1.x与y父节点不相同:此时为不清楚两者关系。
2.x与y父节点相同,rank的值也相同:两者属于同一帮派。
3.x与y父节点相同,rank的值不相同:两者不属于同一帮派。
要得到这个关系,需要在并查集的find函数内对各个点的rank和par的值进行不断的更新。
对于rank,需要使用矢量加法,如
$\overrightarrow{AB}+\overrightarrow {BC}=\overrightarrow {AC}$
结合题目就是例:x的父节点px,如果x与父节点的关系是rank[x],px与父节点的关系是rank[px],那么x与par[px]的关系就是
$(rank[x] + rank[px])\%2$
其他的推理类似。
在find的路径压缩时,注意把rank关系进行更新即可,在union时涉及到x,y与它们的父节点px,py的rank值更新,其实原理相同。
三、AC代码
#include <cstdio>
#include <iostream>
#include <fstream> using namespace std; const int MAXN = 1e5+;
int par[MAXN];
int Rank[MAXN]; void init()
{
for(int i = ; i < MAXN; i++)
{
par[i] = i;
Rank[i] = ;
}
} int find(int x)
{
if(par[x] == x)
return x;
int px = par[x];
par[x] = find(par[x]);
Rank[x] = (Rank[px]+Rank[x])%;
return par[x];
} void Union(int x, int y)
{
int px = find(x);
int py = find(y);
if(px == py)
return;
par[px] = py;
Rank[px] = (Rank[x]++Rank[y])%;
} int main()
{
//freopen("input.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int T, M, N, x, y;
char op; while(scanf("%d", &T)!=EOF)
{ while(T--)
{
init();
scanf("%d %d", &N, &M);
for(int i = ; i < M; i++)
{
getchar();
scanf("%c %d %d", &op, &x, &y);
if(op == 'A')
{
int px = find(x);
int py = find(y);
if(px != py)
{
puts("Not sure yet.");
}
else
{
if(Rank[x] == Rank[y])
puts("In the same gang.");
else
puts("In different gangs.");
}
}
else
{
Union(x, y);
}
}
}
}
return ;
}
POJ_1703 Find them, Catch them 【并查集】的更多相关文章
- poj1703 Find them, Catch them 并查集
poj(1703) Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26992 ...
- POJ 1703 Find them, catch them (并查集)
题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2 D 3 4 D 5 6...这就至少有3个集合了.并且 ...
- poj1703--Find them, Catch them(并查集应用)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32073 Accepted: ...
- POJ1703-Find them, Catch them 并查集构造
Find them, Catch them 好久没有做并查集的题,竟然快把并查集忘完了. 题意:大致是有两个监狱,n个 ...
- 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 并查集的应用
题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...
- POJ-1703 Find them, Catch them(并查集&数组记录状态)
题目: The police office in Tadu City decides to say ends to the chaos, as launch actions to root up th ...
- poj1703_Find them, Catch them_并查集
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42451 Accepted: ...
- 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(并查集高级应用)
手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...
随机推荐
- Hyperledger Fabric Orderer节点启动
Orderer 节点启动通过 orderer 包下的 main() 方法实现,会进一步调用到 orderer/common/server 包中的 Main() 方法. 核心代码如下所示. // Mai ...
- pandas sort_values 排序后, index 也发生了改变,不改变的情况下需要 reset_index(drop = True)
shenpi.sort_values(by=['apply_date'],ascending=True,inplace=True)shenpi.reset_index(drop = True)
- 解决selenium与firefox版本不兼容问题
Python环境下类比 个人使用 32位环境 Python 2.7.12 Selenium 2.53.6 Firefox 47.01 安装selenium可用pip选择对应版本,参考另一教程. 因为在 ...
- LoadRunner使用问题
最近给客户做POC,为了测试大数据的框架的一个并发能力,使用loadrunner进行相关的测试,目前发现几个要注意的地方 1: loadrunner的Java脚本必须使用jdk1.6的32位版本 2: ...
- <abbr> 元素的样式为显示在文本底部的一条虚线边框,当鼠标悬停在上面时会显示完整的文本(只要您为 <abbr> title 属性添加了文本)
<abbr title="World Wide Web">WWW</abbr><br><abbr title="Real Sim ...
- 几个SQL小知识(转)
出处:http://www.cnblogs.com/wuguanglei/p/4205976.html 写在前面的话:之前做的一个项目,数据库及系统整体构架设计完成之后,和弟兄们经过一段时间的编码,系 ...
- c#并发编程经典实例文摘
第1章 并发编程概述 1.1 并发编程简介 并发: 多线程(包括并行处理) 异步编程(异步操作)程序启动一个操作,而该操作将会在一段时间后完成 响应时编程(异步事件)可以没有一个实际的开始,可以在任何 ...
- Struts2获取Action中的数据
当我们用Struts2框架开发时,经常有要获取jsp页面的数据或者在jsp中获取后台传过来的数据(Action),那么怎么去获取自己想要的数据呢? 后台获取前端数据: 在java程序中生成要获取字段的 ...
- 树形DP-----HDU4003 Find Metal Mineral
Find Metal Mineral Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Other ...
- Android在一个app中启动另一个App
Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); Compon ...