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 ...
随机推荐
- Ubuntu下配置Apache的Worker模式
其实Apache本身的并发能力是足够强大的,但是Ubuntu默认安装的是Prefork模式下的Apache.所以导致很多人后面盲目的去 安装lighttpd或者nginx一类替代软件.但是这类软件有一 ...
- linux下方便的录屏命令
linux下方便的录屏命令 ffmpeg -f x11grab -s 1024*768 -r 20 -i :0.0 -sameq ~/recode.mpg -r后是刷新屏率, 推出直接Ctrl ...
- cv 验证
This is how I have trained a xgboost classifier with a 5-fold cross-validation to optimize the F1 sc ...
- CF570D Tree Requests
离线 + 树状数组 如果子树中的一个深度的所有点中有两个以上的字母出现了奇数次,那么这个询问的答案就是$No$,其他的情况吧都是$Yes$. 由于只有$26$个字母,我们可以考虑暴力检验,把树映射到$ ...
- HTTP协议相关介绍
一.HTTP请求方法 根据HTTP标准,HTTP请求可以使用多种请求方法.HTTP1.0定义了三种请求方法: GET, POST 和 HEAD方法.HTTP1.1新增了五种请求方法:OPTIONS, ...
- svn ank问题
subvesion detected a working copy that need upgrade in 可以先在文件夹下cleapup,然后打开解决方案,在解决方案的右键选择upgrade wo ...
- Java 数据结构之双链表
package Linked; public class Mylinked { private Node first;//链表的第一个节点 private Node last;//链表的最后一个节点 ...
- Linux基础-工作中经常使用到的linux 命令
linux 常用命令 (1)命令ls——列出文件 ls -la 给出当前目录下所有文件的一个长列表,包括以句点开头的“隐藏”文件 ls a* 列出当前目录下以字母a开头的所有文件 ls -l *.d ...
- (转)15个非常棒的jQuery无限滚动插件【瀑布流效果】
原文地址:http://www.cnblogs.com/lyw0301/archive/2013/06/19/3145084.html 现在,最热门的网站分页趋势之一是jQuery的无限滚动(也即瀑布 ...
- 20169219 实验二 Java面向对象程序设计
实验二 Java面向对象程序设计 一.程序设计中临时变量的使用 任务: //定义一个数组 int arr[] = {1,2,3,4,5,6,7,8}; //打印原始数组的值 for(int i:arr ...