poj 1703 Find them, Catch them

带权并查集,比食物链简单

Find them, Catch them

Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 27167
Accepted: 8270

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.

Source

POJ Monthly--2004.07.18

[Submit]   [Go Back]   [Status]   [Discuss]


发现它,抓住它(中文版)

  • 查看
  • 提交
  • 统计
  • 提问
总时间限制:1000ms内存限制:65536kB描述
一 个城市中有两个犯罪团伙A和B,你需要帮助警察判断任意两起案件是否是同一个犯罪团伙所为,警察所获得的信息是有限的。假设现在有N起案件 (N<=100000),编号为1到N,每起案件由团伙A或团伙B所为。你将按时间顺序获得M条信息(M<=100000),这些信息分为两 类:
1. D [a] [b]
其中[a]和[b]表示两起案件的编号,这条信息表明它们属于不同的团伙所为

2. A [a] [b]
其中[a]和[b]表示两起案件的编号,这条信息需要你回答[a]和[b]是否是同一个团伙所为
注意你获得信息的时间是有先后顺序的,在回答的时候只能根据已经接收到的信息做出判断。


输入第一行是测试数据的数量T(1<=T<=20)。
每组测试数据的第一行包括两个数N和M,分别表示案件的数量和信息的数量,其后M行表示按时间顺序收到的M条信息。输出对于每条需要回答的信息,你需要输出一行答案。如果是同一个团伙所为,回答"In the same gang.",如果不是,回答"In different gangs.",如果不确定,回答”Not sure yet."。样例输入
1
5 5
A 1 2
D 1 2
A 1 2
D 2 4
A 1 4
样例输出
Not sure yet.
In different gangs.
In the same gang.

我的解答

/*=============================================================================
#     FileName: catch.cpp
#         Desc: poj 1703
#       Author: zhuting
#        Email: [email protected]
#     HomePage: my.oschina.net/locusxt
#      Version: 0.0.1
#    CreatTime: 2013-12-04 19:28:15
#   LastChange: 2013-12-04 19:32:44
#      History:
=============================================================================*/
#include <cstdio>
#include <cstdlib>
#define maxn 100005


class node/*节点类*/
{
	public:
	int fa;/*父节点下标*/
	int re;/*与父节点的关系*/
};

node n[maxn];

int getfa(int x)/*获取父节点下标,顺带更新父节点和与父节点的关系*/
{
	if (n[x].fa == x) 
		return x;
	int tmp = n[x].fa;
	n[x].fa = getfa(tmp);
	if(n[x].re == 1)
		n[x].re = n[tmp].re;
	else 
		n[x].re = !n[tmp].re;
	return n[x].fa;
}

void ask(int x, int y, int num)/*询问x,y的关系*/
{
	if (num == 2 && x != y)/*只有两个案件且不一样,一定different*/
	{
		printf("In different gangs.\n");
		return;
	}
	if (getfa(x) == getfa(y))/*父节点相同时,比较与父节点的关系是否一样*/
	{
		if (n[x].re == n[y].re)
		{
			printf("In the same gang.\n");
			return;
		}
		else 
		{
			printf("In different gangs.\n");
			return;
		}
	}
	printf("Not sure yet.\n");/*否则不确定*/
	return;
}

void diff(int x, int y)/*连接x,y点,且两者关系不同*/
{
	int xf = getfa(x);
	int yf = getfa(y);

	if (xf == yf) return;/*父节点一样,不需要连接,否则会出问题*/
	n[xf].fa = yf;
	if ((n[xf].re == n[x].re) == (n[yf].re == n[y].re))/*更新关系*/
	{
		n[xf].re = 0;
	}
	else
		n[xf].re = 1;
	return;
}

int main()
{
	int t = 0;
	int m = 0, num = 0;
	
	char msg;
	int a = 0, b = 0;
	scanf("%d", &t);
	for (int i = 0; i < t; ++i)
	{

		scanf ("%d%d", &num, &m);
		for (int j = 1; j <= num; ++j)/*init,特别注意范围是1-N,不是0-N-1*/
		{
			n[j].fa = j;
			n[j].re = 1;
		}
		
		for (int j = 0; j < m; ++j)
		{
			scanf ("\n%[AD]%d%d", &msg, &a, &b);
			if (msg == 'A')
				ask(a, b, num);
			else if (msg == 'D')
				diff(a, b);
		}
	}
	return 0;
}




你可能感兴趣的:(poj)