一、题面

POJ1703

二、分析

需要将并查集与矢量法则相结合。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 【并查集】的更多相关文章

  1. poj1703 Find them, Catch them 并查集

    poj(1703) Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26992   ...

  2. POJ 1703 Find them, catch them (并查集)

    题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2  D 3 4 D 5 6...这就至少有3个集合了.并且 ...

  3. poj1703--Find them, Catch them(并查集应用)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32073   Accepted: ...

  4. POJ1703-Find them, Catch them 并查集构造

                                             Find them, Catch them 好久没有做并查集的题,竟然快把并查集忘完了. 题意:大致是有两个监狱,n个 ...

  5. POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

    POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...

  6. POJ 1703 Find them, Catch them 并查集的应用

    题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...

  7. 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 ...

  8. poj1703_Find them, Catch them_并查集

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42451   Accepted: ...

  9. poj.1703.Find them, Catch them(并查集)

    Find them, Catch them Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I6 ...

  10. POJ 1703 Find them, Catch them(并查集高级应用)

    手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...

随机推荐

  1. laravel策略类,实现当前登陆的用户是否具有删除,修改文章的权限

    策略类依赖月门脸类Auth 首先创建一个门脸类 make:auth 然后再创建一个策略  php artisan make:policy PostPolicy 定义Auth的登陆类,用的是哪个模型登陆 ...

  2. Linux常用基本命令 1

    useradd 创建用户. password 修改密码. date 查看时间 man date 帮助文档.f往后翻 b往前翻 q退出.软修改 man hwclock 修改硬件时钟, cal 查看日历 ...

  3. Mask_RCNN caffe2

    cd DETECTRON/detectron/ python2 tools/infer_simple_ip_camera.py \    --cfg configs/12_2017_baselines ...

  4. Hyperledger Fabric源码解析

    Hyperledger Fabric开源于2015年12月,截至2018年2月初有185个公司/组织成员加入.最初由IBM和DAH的工程师贡献,现在约有70名的代码贡献者,4000+代码提交,代码行数 ...

  5. C++函数后面加throw关键字简记

    看代码时候看到fun() throw()的用法,找到一篇blog解释很简单,如下: C++函数后面加关键字throw(something)限制,是对这个函数的异常安全性作出限制. 举例及解释如下: v ...

  6. Ubuntu不能上网解决办法

     一.设置IP.网关.DNS 新安装的Ubuntu系统ifconfig后发现没有ip,所以要设置IP.网关.DNS等,编辑 /etc/networking/interfases sudo vi /et ...

  7. C# winform 打开新窗体 关闭当前窗体

    Form1 的Button 下 { Form2 f2 = new Form2(); f2.ShowDialog(this);// this.Close(); } Form2 的load 下 { //只 ...

  8. Google Earth 8.0

    前几天有看到全新的Google Earth 8.0升级,刚好适合自己的手机应用.Google Earth 8.0 官方下载:https://play.google.com/store/apps/det ...

  9. vs2015+opencv3.3.1+ maxflow-v3.01 c++实现Yuri Boykov 的Interactive Graph Cuts

    出的结果不理想. 感觉是tlink的权重的计算有问题,以及参数的设置.三个可设置参数是后面的 i j k  .如果你找到了一组好参数请告诉我. 下载地址 http://download.csdn.ne ...

  10. react中使用echarts(人物关系图)

    项目中有时会用到echarts,可能不同的框架中语法稍有变通,前几天在react项目中遇到,写此篇以作记录. 不同的charts语法跟支持不同,本篇"echarts": " ...