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.
题意:输入n和m,代表有n个人和m个询问,n个人分别属于两个帮派,每个询问遵循下面的规则:
A a b要求输出a与b是否在同一个帮派。
D a b告诉信息a和b是不同帮派的成员。
不是基本的并查集,可能有多个集合。
比如输入D 1 2 D 3 4 D 5 6...这就至少有3个集合了。
并且任意2个集合之间成员的敌我关系不明。
同一个集合里,关系明确,敌人或朋友。
不能简单认为成朋友在一个集合,敌人在另外一个集合,分成2个集合。
因为题目说只有2个匪帮,很容易进入这个误区。
怎么分类呢?经典的方法,用x,x+n分别代表x属于A类和x属于B类。
怎么合并呢?比如告诉你x和y不在一个集合,那么就是两种可能,x-A,y-B或者x-B,y-A,我们把x,y+n合并,再把y,x+n合并。同一棵树上的时间同时发生或者同时不发生。也就是说,若x-A,则y一定属于B。
怎么查询呢?如果x和y在一棵树上,或者x+n和y+n在一棵树上,就是同一个黑帮的。如果x和y+n在同一棵树上,或者x+n和y在同一棵树上,就是不同的黑帮。否则不能判断。
案例分析:
1
5 5
A 1 2 Not sure yet.
D 1 2
A 1 2 In different gangs.
D 2 4
A 1 4 In the same gang.
参考网站:
https://blog.csdn.net/say_c_box/article/details/50893928
https://blog.csdn.net/yinghui_yht/article/details/53363012
#include
#include
#include
using namespace std;
int fa[200005];//注意数组大小 100005错误runtime error
int find(int x)
{
if(fa[x]==x)
return x;
return fa[x]=find(fa[x]);
}
bool same(int x,int y){
return find(x)==find(y) ;
}
void bind(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
fa[fx]=fy;
}
int main()
{
int t, n,m,x,y;
char c;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m);
for(int i=1;i<=2*n;i++) fa[i]=i;
while(m--)
{
getchar();
scanf(" %c %d %d",&c,&x,&y);
if(c=='A'){
if(same(x,y)||same(x+n,y+n)) //同类
printf("In the same gang.\n");
else if(same(x+n,y)||same(x,y+n))//不同类
printf("In different gangs.\n");
else printf(("Not sure yet.\n"));
}
//D
else {
bind(x,y+n);
bind(x+n,y);
}
}
}
return 0;
}
/*对D的补充说明
x和y不在一个集合
那么就是两种可能,x-A,y-B或者x-B,y-A
我们把x,y+n合并,再把y,x+n合并。
同一棵树上的时间同时发生或者同时不发生。
也就是说,若x-A,则y一定属于B。*/
Runtime Error大概有这几种:
Runtime Error(ARRAY_BOUNDS_EXCEEDED) // array bounds exceed 数组越界
Runtime Error(DIVIDE_BY_ZERO) //divisor is nil 除零
Runtime Error(ACCESS_VIOLATION) //illegal memory access 非法内存读取
Runtime Error(STACK_OVERFLOW) //stack overflow 系统栈过载
具体解决办法:
检查一下数组、指针是否越界;
是否除0;
检查一下小数组是否符合题意,可以把数组开的大一些;
检查一下局部数组变量是否过大。
在此题中我改了一下fa[]数组大小