poj Find them ,Catch them(偏移量应用)

Find them, Catch them
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 38684   Accepted: 11908

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]

Home Page   Go Back  To top

并查集的一个新的解决方法:偏移量,适合分类问题,http://blog.sina.com.cn/s/blog_8747d78801014ojt.html

如何快速确定偏移量公式:

例:现在要合并节点x,y, 找到根节点fx = Find(x); fy = Find(y);一般情况下,根节点的偏移量都保持为0, offset[foot] = 0;如果要使得x和y的偏移量为t,假设fx指向fy,则可以写出公式offset[x] + offset[fx] - offset[y] = t,则offset[fx] = (offset[y] + t - offset[x]) % n; 这个n即为总共有多少类,如:在poj1182 食物链中n = 3,,在poj2492 A Bug's Life中n = 2, 这样fx的偏移量就计算出来了,只需要改其中一个根节点的偏移量,这里是fx,因为假设是fx指向fy


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int pre[110000],ran[110000]; //对父节点的偏移量,0表相同,1表不同 
//路径压缩,同时修改途中各点对父节点的偏移量 
int find(int x)
{
	int f=pre[x];
	if(x==pre[x])
	return x;
    pre[x]=find(pre[x]);//x直接指向根节点,路径压缩
    //x现在指向根节点,x对父节点的偏移量就是对根节点的偏移量
    ran[x]=(ran[x]+ran[f])%2;// 类别偏移,x现在指向根节点
    return pre[x];
 } 
void merge(int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)
	{
		pre[fy]=fx;
		ran[fy]=(ran[x]+ran[y]+1)%2;//类别偏移,fy相对于父节点fx的偏移量 
	}
	
}
int k,i,l,m,n,p,b,c;
char a;
int main()
{
	scanf("%d",&p);
	while(p--)
	{
		scanf("%d%d",&m,&n);
		getchar();
		for(i=1;i<=m;i++)
		{
			pre[i]=i;
			ran[i]=0;//对自己的偏移量为0 
		}
		for(i=0;i<n;i++)
		{
			scanf("%c %d %d",&a,&b,&c);
			int fx=find(b);
			int fy=find(c);
	    	getchar();
	    	if(a=='A') 
			{
				if(fx!=fy)
				printf("Not sure yet.\n");
				
				else if(ran[b]==ran[c])
				printf("In the same gang.\n");
				else 
				printf("In different gangs.\n");
			}
	    	else 
	    	{
	    		merge(b,c);
			}	
		}	
	}
}


你可能感兴趣的:(poj Find them ,Catch them(偏移量应用))