一、题面

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. C++中内存区域的划分

    栈存储区 那些由编译器在需要的时候分配,在不需要的时候自动清楚的变量的存储区.里面的变量通常是局部变量.函数参数等. 堆存储区(自由存储区) 那些由new或者malloc分配的内存块,他们的释放编译器 ...

  2. jqentitydetail

    using System;using System.Collections;using System.Collections.Generic;using System.Linq;using Syste ...

  3. Maven——继承和聚合

    实际项目中,可能正要构建一个大型的系统,但又不想一遍又一遍的重复同样的依赖元素,这种情况是经常出现的.不过还好,maven提供了继承机制,项目可以通过parent元素使用继承,可以避免这种重复.当一个 ...

  4. C#使用var定义变量时的四个特点

    使用var定义变量时有以下四个特点: 1. 必须在定义时初始化.也就是必须是var s = “abcd”形式: 2. 一但初始化完成,就不能再给变量赋与初始化值类型不同的值了. 3.   var要求是 ...

  5. HDU 4111 Alice and Bob (博弈+记忆化搜索)

    题意:给定 n 堆石头,然后有两种操作,一种是把从任意一堆拿走一个,另一种是把一个石子放到另一堆上. 析:整体看,这个题真是不好做,dp[a][b] 表示有 a 堆1个石子,b个操作,操作是指把其他的 ...

  6. vmware ubuntu安装vmware tools

    vmware tools可以说是其平台虚拟机必不可少的工具,可以使母机(你的电脑)通过复制粘贴向虚拟机中传递文件信息,对我们虚拟机的使用由非常大的帮助,当然也可以通过使用共享硬盘来共享文件,但操作起来 ...

  7. Android 应用检查更新并下载

    1.在Android应用当中都有应用检查更新的要求,往往都是在打开应用的时候去更新下载. 实现的方法是:服务器端提供接口,接口中可以包含在最新APK下载的URL,最新APK的VersionCode,等 ...

  8. 【转】Android ActionBar完全解析,使用官方推荐的最佳导航栏(上)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/18234477 本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工 ...

  9. owinAuthorize

    Nuget包获取 Install-Package Microsoft.AspNet.WebApi.Owin -Version 5.1.2 Install-Package Microsoft.Owin. ...

  10. raiden_graph

    使用mermaid描述 raiden 通道 AB,正常状态 graph LR A-- 60,100,S_100 ---B 通道 AB closed graph LR A((A)) -. 60,100 ...