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.
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.
题意 : 有两个犯罪团伙, n宗罪
输入D 时 说明后面两宗罪属于不同团伙的
输入A 时 要求你做出判断 输出 Not sure yet./ In different gangs. / In the same gang.
思路: 有关联的并查集
set数组储存 根节点, rel 数组储存与跟节点的关系, rel=0 说明在同一伙, =1 在不同伙
对rel 的 值的判断运用了向量的思想~因为只存在两种关系, 所以任意三个数的rel 值可以构成向量三角形.
即 如果 a 和 b 的关系是 r1, b 和 c 的关系是 r2, 那么 a 和 c 的关系就是 (r1+r2)%2 . PS:因为只用两种情况所以对 2 取模。
注意在判断两个数是否为同伙是应该用两个数的根节点去判断, 根节点相同, 则说明这两个数已经判断过了, 不同的话输出 Not sure yet.
code;
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int set[100005];
int rel[100005];
int find(int x)
{
int root;
if(set[x]==x)return x;
root=find(set[x]);
rel[x]=(rel[x]+rel[set[x]])%2;//逐个修改权值,逐个递推直到与根节点的关系~
return set[x]=root;
}
int main()
{
// freopen("F.txt","r",stdin);
int T,n,m;
scanf("%d",&T);
{
//getchar();
int a,b;
char c;
while(T--)
{
memset(rel,0,sizeof(rel));
scanf("%d%d%*c",&n,&m);
//getchar();
for(int i=1;i<=n;i++)
set[i]=i;
for(int i=1;i<=m;i++)
{
scanf("%c%d%d%*c",&c,&a,&b);
//getchar();
if(c=='A')
{
if(find(a)!=find(b))printf("Not sure yet.\n");
else
{
if(rel[a]!=rel[b])printf("In different gangs.\n");
else printf("In the same gang.\n");
}
}
if(c=='D')
{
int fa=find(a),fb=find(b); //更改根节点, 更改权
if(fa!=fb)
{
set[fb]=fa;
rel[fb]=(rel[a]-rel[b]+1)%2;
//得到两根节点的关系 fa,fb~
}
}
}
}
}
return 0;
}