Find them, Catch them

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 49867 Accepted: 15320

Description

The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present question is, given two criminals; do they belong to a same clan? You must give your judgment based on incomplete information. (Since the gangsters are always acting secretly.)

Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds:

  1. D [a] [b]

    where [a] and [b] are the numbers of two criminals, and they belong to different gangs.

  2. A [a] [b]

    where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang.

Input

The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. Each test case begins with a line with two integers N and M, followed by M lines each containing one message as described above.

Output

For each message “A [a] [b]” in each case, your program should give the judgment based on the information got before. The answers might be one of “In the same gang.”, “In different gangs.” and “Not sure yet.”

Sample Input

1

5 5

A 1 2

D 1 2

A 1 2

D 2 4

A 1 4

Sample Output

Not sure yet.

In different gangs.

In the same gang.


解题心得:

  1. 有两个帮派,龙帮和蛇帮,有n个罪犯,每个罪犯属于其中一个帮派,有m个操作,第一种就是规定罪犯a,b不属于一个帮派,第二种就是询问罪犯a,b的关系。
  2. 刚开始看这个题的时候真的很蒙,写得暴力会TLE,后来看大佬说有两种方法,
    • 第一种,我把它称为创造镜像(a的镜像是a+n),每个罪犯的镜像和自己属于敌对关系,这样就可以把并查集分成两个部分,两个敌对关系的集合,如果给出a和b是敌对关系,那么就可以合并a和b镜像,合并b和a的镜像。
    • 第二种是直接观察关系的转变,在find father和merge时候维护关系,首先使用cm[i]来表示第i个罪犯的帮派(值为0,1),先全部初始化为0,如果a和b是敌对关系,那么cm[b] = (cm[a]+1+cm[b])%2,在find father的时候需要从祖先节点开始从上往下维护,可以发现cm[son] = (cm[son] + cm[father])%2,这里可以仔细想想这个关系,要明白的是我们将所有的帮派初始化都为0。

第一种方法:

#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn = 3e5+100;
int father[maxn],n,m,t; int find(int x){
if(father[x] == x)
return x;
return father[x] = find(father[x]);
} void merge(int x,int y) {
int fx = find(x);
int fy = find(y);
father[fy] = fx;
} bool _same(int x,int y) {
return find(x) == find(y);
} void unite(int x,int y) {
int fx = find(x);
int fy = find(y);
father[fy] = fx;
} void init() {
scanf("%d%d",&n,&m);
for(int i=1;i<=n*3;i++)
father[i] = i;
} void Solve() {
for(int i=0;i<m;i++) {
char temp[5];
int a,b;
scanf("%s%d%d",temp,&a,&b);
if(temp[0] == 'A') {
if(find(a) == find(b)) {
printf("In the same gang.\n");
} else if(_same(a,b+n) && _same(a+n,b)) {
printf("In different gangs.\n");
} else
printf("Not sure yet.\n");
} else {
unite(a,b+n);
unite(a+n,b);
}
}
} int main() {
scanf("%d",&t);
while(t--) {
init();
Solve();
}
return 0;
}

第二种方法:

#include <algorithm>
#include <stdio.h>
#include <cstring>
using namespace std;
const int maxn = 1e5+100;
int father[maxn],n,m,cm[maxn]; void init() {
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
father[i] = i;
memset(cm,0,sizeof(cm));
} int find(int x) {
if(x == father[x])
return x;
int temp = father[x];
father[x] = find(father[x]);
cm[x] = (cm[temp]+cm[x])%2;
return father[x];
} void Unite(int a,int b) {
int fa = find(a);
int fb = find(b);
if(fa == fb)
return;
father[fb] = fa;
cm[fb] = (cm[a] + 1 - cm[b]) % 2;
} int main() {
int t;
scanf("%d",&t);
while(t--){
init();
while(m--) {
char temp[5];
int a,b;
scanf("%s%d%d",temp,&a,&b);
if(temp[0] == 'A') {
if (find(a) != find(b)) {
printf("Not sure yet.\n");
} else if (cm[a] == cm[b]) {
printf("In the same gang.\n");
} else
printf("In different gangs.\n");
} else {
Unite(a,b);
}
}
}
return 0;
}

POJ:1703-Find them, Catch them(并查集好题)(种类并查集)的更多相关文章

  1. POJ 1703 Find them, Catch them(确定元素归属集合的并查集)

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

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

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

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

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

  4. POJ 1703 Find them, Catch them(种类并查集)

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

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

  6. 并查集例题01. 种类并查集(poj1733)

    题目: http://poj.org/problem?id=1733 题意: 输入n表示有一个长度为n的0,1字符串, m表示接下来有m行输入, 接下来的m行输入中x, y, even表示第x到第y个 ...

  7. BZOJ 1370: [Baltic2003]Gang团伙 [并查集 拆点 | 种类并查集WA]

    题意: 朋友的朋友是朋友,敌人的敌人是朋友:朋友形成团伙,求最多有多少团伙 种类并查集WA了一节课,原因是,只有那两种关系才成立,诸如朋友的敌人是朋友之类的都不成立! 所以拆点做吧 #include ...

  8. POJ1417 True Liars 并查集 动态规划 (种类并查集)

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ1417 题意概括 有一群人,p1个好人,p2个坏人. 他们说了n句话.(p1+p2<=600,n ...

  9. [并查集] POJ 1703 Find them, Catch them

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

  10. hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them

    http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...

随机推荐

  1. 如何让MVC和多层架构和谐并存(二)

    上一节说了一些笼统的东西,这节说一些实际的操作. 1.取列表.这是一个新闻列表:         对应MVC的model是: public class NewsListModel { /// < ...

  2. 快速提取邮箱地址(利用word或网站)

    在word中,CTRL+F,输入:[A-z,0-9]{1,}\@[A-z,0-9,\.]{1,} 点击“高级”,勾选“使用通配符”,点击“查找全部”: 复制.粘贴. 还可通过以下页面在线提取. htt ...

  3. webpack踩坑

    1.当你用webpack2实现css文件单独成一个文件的时候: 可能遇到这种错误Error: Breaking change: extract now only takes a single argu ...

  4. hdu-2852 KiKi's K-Number---二分+树状数组

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2852 题目大意: 题意:    给出三种操作,    0 在容器中插入一个数.    1 在容器中删 ...

  5. 软件的依赖关系:类图关系:is-a has-a use-a

    基本描述: 类图关系:is-a has-a use-a: 依赖规则: 依赖倒置: 依赖注入: 接口隔离: 无环依赖: 稳定依赖: 依赖倒置描述: 底层抽象-->高层抽象 ^          ^ ...

  6. 【BZOJ5212】[ZJOI2018] 历史(LCT大黑题)

    点此看题面 大致题意: 给定一棵树每个节点\(Access\)的次数,求最大虚实链切换次数,带修改. 什么是\(Access\)? 推荐你先去学一学\(LCT\)吧. 初始化(不带修改的做法) 首先考 ...

  7. MCV 的几种表单提交方式

    一,MVC  HtmlHelper方法 Html.BeginForm(actionName,controllerName,method,htmlAttributes){}   其中actionName ...

  8. 旧文备份:rtlinux安装手册

    前段时间接触了几天RTLinux,折腾了好几天才总算把它安装上,得益于Prof. Chang-Gun Lee的安装建议,觉得该文档可能会对准备尝试安装RTLinux的朋友们有帮助,本人英语很烂,也比较 ...

  9. 旧文备份:怎样利用好单片机上的存储器资源来实现OD的存储与访问

    我们知道OD(对象字典)是CANopen的核心,所有功能都是围绕它开展的,是协议栈的数据中心,良好的OD实现是协议栈高效稳定运行的基础,而OD的实现最基本的一点就是怎么去保存它.因为OD的内容比较杂, ...

  10. Vue nodejs商城项目-搭建express框架环境

    1.express-project 搭建express框架环境 安装express generator生成器 通过生成器自动创建项目 配置分析 安装 cnpm i -g express-generat ...