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:
-
D [a] [b]
where [a] and [b] are the numbers of two criminals, and they belong to different gangs. -
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.
书上的看不懂,网上找了好多才有这一个懂的。。。这个比较厉害,书上的也尽量看懂吧。
转至
现在终于把以前并查集遗留的一点问题弄明白了,一下午做一题,值了。
#include<stdio.h>
#include<string.h>
#define maxn 505
int a[130000],v[130000];
int find(int x){
return x==a[x]?x:(a[x]=find(a[x]));
}
void join(int x,int y){
int fx=find(x);
int fy=find(y);
if(fx!=fy)
a[fx]=fy;
}
int main()
{
int n,m,k,i,j,s,x,y;
char p;
scanf("%d",&k);
while(k--){
scanf("%d %d",&n,&m);
memset(v,0,sizeof(v));
for(i=0;i<=n;i++)
a[i]=i;
while(m--){
scanf(" %c%d%d",&p,&x,&y);
if(p=='A'){
if(find(x)==find(v[y])) printf("In different gangs.\n");
else if(find(x)==find(y)) printf("In the same gang.\n");
else printf("Not sure yet.\n");
}
else {
if(v[x]) join(v[x],y);//两个敌人添加到一队
if(v[y]) join(v[y],x);
v[x]=y;//相互是敌人
v[y]=x;
}
}
}
return 0;
}
还有两种新的方法:
转至
对每个顶点x,用x表示属于A帮派,x+n表示属于B帮派(数组大小2*n)。
当x和y属于不同帮派时,则x+n和y属于同一个帮派,x和y+n属于同一个帮派。
三种情况:
- 当x和y在同一集合时(即x和y所在集合根节点相同),则x和y在同一帮派。
- 当x和y+n或x+n和y在同一集合时,则x和y不在同一帮派。
- 其他情况(即当x和y不在同一集合时),则x和y帮派不确定。
#include<stdio.h>
#include<string.h>
#define maxn 505
int a[130000*3];
int find(int x){
return x==a[x]?x:(a[x]=find(a[x]));
}
void join(int x,int y){
int fx=find(x);
int fy=find(y);
if(fx!=fy)
a[fx]=fy;
}
int same(int x,int y){
if(find(x)==find(y)) return 1;
else return 0;
}
int main()
{
int n,m,k,i,j,s,x,y;
char p;
scanf("%d",&k);
while(k--){
scanf("%d %d",&n,&m);
for(i=0;i<=n*3;i++)
a[i]=i;
while(m--){
scanf("\n%c%d%d",&p,&x,&y);
if(p=='A'){
if(same(x+n,y)||same(x,y+n)) printf("In different gangs.\n");
else if(same(x,y)) printf("In the same gang.\n");
else printf("Not sure yet.\n");
}
else {
join(x+n,y);
join(x,y+n);
}
}
}
return 0;
}
其实还是不特别明白。
#include<stdio.h>
#include<string.h>
#define maxn 130000
int a[130000],rel[130000];
int find(int x){
int temp=a[x];
if(x==a[x]) return x;
a[x]=find(a[x]);
rel[x]=(rel[x]==rel[temp])?0:1;
return a[x];
}
void join(int x,int y,int fx,int fy){
a[fy]=fx;
rel[fy]=(rel[x]==rel[y])?1:0;
}
int main()
{
int n,m,k,i,j,s,x,y;
char p;
scanf("%d",&k);
while(k--){
scanf("%d %d",&n,&m);
for(i=0;i<=n;i++)
a[i]=i;
memset(rel,0,sizeof(rel));
while(m--){
scanf("\n%c%d%d",&p,&x,&y);
int fx=find(x),fy=find(y);
if(p=='A'){
if(fx==fy){
if(rel[x]==rel[y]) printf("In the same gang.\n");
else printf("In different gangs.\n");
}
else printf("Not sure yet.\n");
}
else {
if(fx!=fy) join(x,y,fx,fy);
}
}
}
return 0;
}