Find them, Catch them
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 42463   Accepted: 13065

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.

思路

题意:有两个不同的帮派,每个帮派至少有一人。判断两人是否是同一帮派的。
题解:食物链的简化版
方法一:
因为有两个帮派,因此对于每个人只要创建 2 个元素 i - A,i - B,并利用 2*N 个元素建立并查集。
假设 x , y属于不同的帮派,x , y + N 则是同一个帮派,x + N , y 同理。因此只需要将(x , y + N) 和 (x + N , y) 合并即可。
方法二:带权并查集,利用r[ ]数组记录每个元素与其父亲节点的关系。
r[ x ] = 0 代表 x 与其父亲节点是同一个帮派的;
r[ x ] = 1 代表 x 与其父亲节点是敌对帮派的;
一开始每个人都是自己的父亲节点 f[ x ] = x,每个人与自己的关系都是同属于一个阵营 r[ x ] = 0;
1、find( ) 函数寻找根节点的时候要不断更新 r[ ]数组
根据子节点与父节点的关系和父节点和爷爷节点的关系推到子节点和爷爷节点的关系。
很容易通过穷举发现其关系式:a 和 b 的关系为 r1, b 和 c 的关系为r2,则 a 和 c 的关系为: r3 = ( r1 + r2) % 2;
 
(爷爷,父亲) (父亲,儿子) (爷爷,儿子)
0 0 0
0 1 1
1 0 1
1 1 0

2、 Union的时候更新两棵树的关系

定义:fx 为 x的根节点, fy 为 y 的根节点,联合时,使得fa[ fx ] = fy;同时也要寻找 fx 和 fy 的关系,其关系为(r[ x ]  + 1 - r[ y ]) % 2;因为确定了 x 和 y 的关系是 1 ,因此 r[ fy ] = (r[ x ] + 1 - r[ y ]) % 2;
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 100005;
int fa[maxn*3];

int find(int x)
{
	int r = x;
	while (r != fa[r])	r = fa[r];
	int i = x,j;
	while (i != r)
	{
		j = fa[i];
		fa[i] = r;
		i = j;
	}
	return r;
}

void unite(int x,int y)
{
	x = find(x),y = find(y);
	if (x != y)	fa[x] = y;
}

bool same(int x,int y)
{
	return find(x) == find(y);
}

int main()
{
	int T;
	scanf("%d",&T);
	while (T--)
	{
		int N,M,x,y;
		char opt[5];
		scanf("%d%d",&N,&M);
		for (int i = 0;i <= 3*N;i++)	fa[i] = i;
		while (M--)
		{
			scanf("%s %d %d",opt,&x,&y);
			if (opt[0] == 'A')
			{
				if (find(x) == find(y))
					printf("In the same gang.\n");
				else if (same(x,y + N) && same(x + N,y))
					printf("In different gangs.\n");
				else
					printf("Not sure yet.\n");
			}
			else if (opt[0] == 'D')
			{
				unite(x,y + N);
				unite(x + N,y);
			}
		}
	}
	return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 100005;
int fa[maxn],r[maxn];

int find(int x)
{
	if (fa[x] == x)	return fa[x];
	int tmp = fa[x];
	fa[x] = find(fa[x]);
	r[x] = (r[tmp] + r[x]) % 2;
	return fa[x];
}

void unite(int x,int y)
{
	int fx = find(x),fy = find(y);
	if (fx == fy)	return;
	fa[fy] = fx;
	r[fy] = (r[x] + 1 - r[y]) % 2;
}

int main()
{
	int T;
	scanf("%d",&T);
	while (T--)
	{
		int N,M,x,y;
		char opt[5];
		scanf("%d%d",&N,&M);
		for (int i = 0;i <= N;i++)	fa[i] = i,r[i] = 0;
		while (M--)
		{
			scanf("%s %d %d",opt,&x,&y);
			if (opt[0] == 'A')
			{
				if (find(x) == find(y))
				{
					if (r[x] == r[y])	printf("In the same gang.\n");
					else	printf("In different gangs.\n");
				}
				else	printf("Not sure yet.\n");
			}
			else	unite(x,y);
		}
	}
	return 0;
}

  

POJ 1703 Find them, Catch them(带权并查集)的更多相关文章

  1. 【POJ 1984】Navigation Nightmare(带权并查集)

    Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40 ...

  2. POJ 1984 Navigation Nightmare 【经典带权并查集】

    任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K To ...

  3. [poj 2912] Rochambeau 解题报告 (带权并查集)

    题目链接:http://poj.org/problem?id=2912 题目: 题目大意: n个人进行m轮剪刀石头布游戏(0<n<=500,0<=m<=2000) 接下来m行形 ...

  4. poj 1733 Parity game【hash+带权并查集】

    hash一下然后用带权并查集做模2下的前缀和 #include<iostream> #include<cstdio> #include<map> #include& ...

  5. POJ 2492 A Bug's Life 带权并查集

    题意: 思路: mod2 意义下的带权并查集 如果两只虫子是异性恋,它们的距离应该是1. 如果两只虫子相恋且距离为零,则它们是同性恋. (出题人好猥琐啊) 注意: 不能输入一半就break出来.... ...

  6. 【poj 1182】食物链(图论--带权并查集)

    题意:有3种动物A.B.C,形成一个"A吃B, B吃C,C吃A "的食物链.有一个人对N只这3类的动物有M种说法:第一种说法是"1 X Y",表示X和Y是同类. ...

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

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

  8. 【POJ 1988】 Cube Stacking (带权并查集)

    Cube Stacking Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)id ...

  9. POJ 1703 Find them, Catch them (数据结构-并查集)

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

  10. POJ - 3728:The merchant (Tarjan 带权并查集)

    题意:给定一个N个节点的树,1<=N<=50000 每个节点都有一个权值,代表商品在这个节点的价格.商人从某个节点a移动到节点b,且只能购买并出售一次商品,问最多可以产生多大的利润. 思路 ...

随机推荐

  1. 一个语句创建Oracle所有表的序列

    -- 动态创建序列 declare cursor c_job is select TABLE_NAME from user_tables; c_row c_job%rowtype; v_sql ); ...

  2. ORACLE判别字段是否包含中文

    在ORACLE数据库中如何查找那些字段里面包含中文的数据记录呢,有时候就是有这样的特殊需求,下面整理了一些判别字段中包含中文记录的几个方法 1:使用ASCIISTR函数判别   ASCIISTR函数说 ...

  3. JVM之SerialOld收集器

    Serial收集器的老年代版本 单线程收集器 标记-整理算法 stop the world Client模式下的虚拟机使用 Server模式下,搭配Parallel Scavenge使用及CMS发生C ...

  4. WinForm常用属性

    Text: 字符串,窗体标题 MaximizeBox: 布尔, 窗体能否最大化 MinimizeBox: 布尔,窗体能否最小化 ShowIcon: 布尔,左上角图标 ShowInTaskbar: 布尔 ...

  5. 实时事件统计项目:优化flume:用file channel代替mem channel

    背景:利用kafka+flume+morphline+solr做实时统计. solr从12月23号开始一直没有数据.查看日志发现,因为有一个同事加了一条格式错误的埋点数据,导致大量error. 据推断 ...

  6. macOS安装Solr并索引MySQL

    安装 Java 语言的软件开发工具包 brew cask install java 或者在 Oracle官网 中选择 Mac 版本 jdk-8u111-macosx-x64.dmg 下载并安装. 安装 ...

  7. android intent 传递list或者对象

    (转:http://www.cnblogs.com/lee0oo0/archive/2012/09/24/2699805.html) 方法一: 如果单纯的传递List<String> 或者 ...

  8. 浅谈Linux中的信号处理机制(一)

    有好些日子没有写博客了,自己想想还是不要荒废了时间,写点儿东西记录自己的成长还是百利无一害的.今天是9月17号,暑假在某家游戏公司实习了一段时间,做的事情是在Windows上用c++写一些游戏英雄技能 ...

  9. Python+selenium自动化脚本编辑过程中遇到的问题和小技巧

    应该也不算是问题和技巧,算是实践中学习到的Python,记录下,也不定时更新 1.通过截取url判断 实例: self.assertEqual(self.broswer.current_url[sel ...

  10. Linux档案与目录管理

    Linux档案与目录管理1. 目录与路径1.1 相对路径与绝对路径1.2 目录的相关操作: cd, pwd, mkdir, rmdir cd [相对路径或绝对路径]cd ~ [用户]: 切换家目录cd ...