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

[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: cnjs.zhuting@gmail.com
#     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;
}




转载于:https://my.oschina.net/locusxt/blog/181522

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值