一、题面

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. 118. Pascal's Triangle杨辉三角形(全部/一行)

    [抄题]: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...

  2. 我使用的网址--Hadoop

    1.Hadoop 官网下载:http://hadoop.apache.org/releases.html 各版本网址:http://mirror.bit.edu.cn/apache/hadoop/co ...

  3. MySQL——explain性能分析的使用

    用法:explain sql语句: id:查询的序号. ref:进行连接查询时,表得连接关系.可以通过上图看出. select_type:select查询的类型,主要是区别普通查询和联合查询.子查询之 ...

  4. combogrid change check multiple

    this.SetDict = function (obj, dicType, multiple, Ischeckbox, callback, change) { obj.combogrid({ pan ...

  5. VC中多线程(一)Win32 API对多线程编程的支持

    http://blog.sina.com.cn/s/blog_4ae08ad801008yer.html

  6. input 框提示信息

    给input添加提示信息,只需添加 “placeholder”的class,将提示信息放在value中, 其中“placeholder”的名字是随便取的,不是H5的“placeholder”属性 例子 ...

  7. javascript总结4:javascript常见变量

    1 javascript变量 定义:变量在计算机中,是用于存储信息的"容器". 2  使用方法: 2-1 定义变量: var n1; 2-2 变量赋值: var n2=23.45; ...

  8. C# JackLib系列之字体使用

    字体的使用一般我们都是使用系统字体,这样比较方便,直接 Font font=new Font("微软雅黑",16f,FontStyle.Bold); 但是当我们用到一个系统没有的字 ...

  9. 设计模式08: Composite 组合模式(结构型模式)

    Composite 组合模式(结构型模式) 对象容器的问题在面向对象系统中,我们常会遇到一类具有“容器”特征的对象——即他们在充当对象的同时,又是其他对象的容器. public interface I ...

  10. Entity Framework快速入门--直接修改(简要介绍ObjectContext处理机制)

    在介绍Entity Framework的修改实体到数据库的方法之前呢,我们先简要的介绍一下ObjectContext的处理机制. 1.ObjectContext的处理机制 ObjectContext是 ...