## 并查集小结(一) ##
- POJ 1703
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.
题意大概是先输入强盗人数和操作次数,每次输入一个operation或者query,op是把两个强盗分到两边,query是询问当前两个强盗是不是一组的。
两个集合,众多子元素,询问每两者之间非一即二的关系,很容易就想到了并查集。
我是开了一个长为2*maxn+5的数组,maxn为最多输入的子元素个数。【1,maxn】为一组,【maxn+1,2*maxn】为二组,因为无法确认输入的两个int,哪个是一组,哪个是二组,故merg(合并)两次。这样处理后结果就是a的敌人和a朋友的敌人,a,和a的朋友都有同样根节点,这样就很容易明白后面的判断了。
- 调用finde函数时,有两种方法缩短路径。
int finde(int a)
{
int k = a,m;
while(a != father[a])
{
a = father[a];
}
while(k != father[a])
{
m = father[k];
father[k] = a;
k = m;
}
return father[a];
}
这样的好处是避免了递归(即栈溢出),从下往上一层层访问并修改该集维护的value值
int find(int x){
if(father[x]!=x){
int tmp=father[x];
father[x]=find(father[x]);
dis[x]+=dis[tmp];
}
return father[x];
}
这样写的好处是从上往下维护value值。
剩下的就没什么难理解。。。
最后,贴上我的AC代码。如果各位大神觉得这篇文章还说的过去,对自己有启发的话,不放点个赞吧。
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
int n;
const int maxn = 100005;
int father[2*maxn + 5];
int finde(int a)
{
int k = a,m;
while(a != father[a])
{
a = father[a];
}
while(k != father[a])
{
m = father[k];
father[k] = a;
k = m;
}
return father[a];
}
void merg(int a,int b)
{
int p = finde(a);
int q = finde(b);
if(p == q) return;
else father[p] = q;
return;
}
bool judge(int a,int b)
{
int q = finde(a);
int p = finde(b);
if(p == q) return true;
else return false;
}
void init()
{
for(int i=1;i<=n;i++)
{
father[i] = i;
}
for(int i=1+maxn;i <= maxn+n;i++)
{
father[i] = i;
}
return;
}
int main()
{
int t,m;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
for(int j=0;j<m;j++)
{
char op;
getchar();
scanf("%c",&op);
if(op == 'D')
{
int x,y;
scanf("%d%d",&x,&y);
merg(x,y+maxn);
merg(y,x+maxn);
}
else if(op == 'A')
{
int x,y;
scanf("%d%d",&x,&y);
if(judge(x,y+maxn)) printf("In different gangs.\n");
else if(judge(x,y)) printf("In the same gang.\n");
else printf("Not sure yet.\n");
}
}
}
return 0;
}