刚学完并查集,对并查集有了初步的认识,小记一番
就拿这个图来说吧,如果说现在我想知道3和5是不是亲戚,我可以通过判断他们两个是不是一个祖先来判断,那么并查集的思想就是说,如果3和5有同一个祖先,那么就认为3和5是亲戚。
这样在解决实际问题的时候就简化了不少,不过如果说我们面对的是庞大的数据的话,这样运算还是会很麻烦的,这样就会想怎么才能去优化我们的这种思路。
既然9是3的祖先,那么我们可以直接让9和3相连,这样在判断3和5是不是亲戚的时候,就不用再让3通过4判断了,这样问题就简化了不少,因为反正最后也要进行这一步操作的!
这一步也可以叫做压缩路径。
在刚开始接触别人写的板子的时候,发现用father[i]作为判断子节点的父节点的名字还挺好...........
扯远了~~~~
不过这一步可以理解为找祖先,直接把两个关联起来
int Find(int x)
{
while(x!=father[x])
x=father[x];
return x;
}
int Find(int x)
{
if(x!=father[x])
father[x]=Find(father[x]);
return father[x];
}
这里暂且给出两个找祖先的方法。
到这里,算是把子代与祖先那一代直接连接起来了!
接下来要做的事情就是合并了。
那么问题也就来了,我们要合并什么?
举个例子:A是B的父亲,B是C的父亲,问A和C是不是亲戚
刚才我们找祖先那一步的意思是说,对于A,B,C我们找到他们的祖先,用fa,fb,fc表示,表示完之后,我们要合并fa,fb,fc
也就是说father[fb]=fa,father[fc]=fb;
那么接下来就有一个关系father[father[fc]]=f[a]。
这样问题就很清楚了,A和C是亲戚!
上面说的接下来的那个关系就是我们后来判断结果的一个条件,关键是怎么使用这个条件。
上面还有一个关键点,就是赋初值!!!!
刚开始的时候让每个点与其父节点的值相等。
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others.
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.
Input
The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
Output
For each case, output the number of suspects in one line.
Sample Input
100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
200 2
1 5
5 1 2 3 4 5
1 0
0 0
Sample Output
4
1
1
直接给出代码:
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int father[30005];
int rannd[30005];
int N,m,n,x,y;
int Find(int x)
{
if(x!=father[x])
father[x]=Find(father[x]);
return father[x];
}
int main()
{
while(~scanf("%d%d",&N,&m)&&N||m)
{
if(m==0)
{
printf("1\n");
continue;
}
for(int i=0;i<N;++i)
{
father[i]=i;
}
memset(rannd,0,sizeof(rannd));
while(m--)
{
scanf("%d%d",&n,&x);
n--;
while(n--)
{
scanf("%d",&y);
int fx=Find(x);
int fy=Find(y);
if(rannd[fx]>rannd[fy])
{
father[fy]=fx;
}
else
{
father[fx]=fy;
if(rannd[fx]==rannd[fy])
{
rannd[fy]++;
}
}
}
}
int ans=0;
int xxx=Find(0);
for(int i=0;i<N;++i)
{
if(Find(i)==xxx)
{
++ans;
}
}
printf("%d\n",ans);
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int N,n,m;
int x,y;
int father[30005];
int Find(int x)
{
if(x!=father[x])
father[x]=Find(father[x]);
return father[x];
}
void combine(int a,int b)
{
int fa=Find(a);
int fb=Find(b);
if(fa!=fb)
father[fa]=fb;
}
int main()
{
while(~scanf("%d%d",&N,&m)&&(m+N))
{
memset(father,0,sizeof(father));
for(int i=0;i<N;++i)
{
father[i]=i;
}
while(m--)
{
scanf("%d %d",&n,&x);
n--;
while(n--)
{
scanf("%d",&y);
combine(x,y);
}
}
int num=0;
for(int i=0;i<N;++i)
{
if(Find(0)==Find(i))
++num;
}
printf("%d\n",num);
}
return 0;
}
第一个比第二个简单,但是第二个比第一个更加优化!
下面再给出一个例题,通过这道题进行一个解释 POJ 2236
An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The computers are repaired one by one, and the network gradually began to work again. Because of the hardware restricts, each computer can only directly communicate with the computers that are not farther than d meters from it. But every computer can be regarded as the intermediary of the communication between two other computers, that is to say computer A and computer B can communicate if computer A and computer B can communicate directly or there is a computer C that can communicate with both A and B.
In the process of repairing the network, workers can take two kinds of operations at every moment, repairing a computer, or testing if two computers can communicate. Your job is to answer all the testing operations.
Input
The first line contains two integers N and d (1 <= N <= 1001, 0 <= d <= 20000). Here N is the number of computers, which are numbered from 1 to N, and D is the maximum distance two computers can communicate directly. In the next N lines, each contains two integers xi, yi (0 <= xi, yi <= 10000), which is the coordinate of N computers. From the (N+1)-th line to the end of input, there are operations, which are carried out one by one. Each line contains an operation in one of following two formats:
1. "O p" (1 <= p <= N), which means repairing computer p.
2. "S p q" (1 <= p, q <= N), which means testing whether computer p and q can communicate.
The input will not exceed 300000 lines.
Output
For each Testing operation, print "SUCCESS" if the two computers can communicate, or "FAIL" if not.
Sample Input
4 1
0 1
0 2
0 3
0 4
O 1
O 2
O 4
S 1 4
O 3
S 1 4
Sample Output
FAIL
SUCCESS
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
struct xiao
{
int x,y;
}beginn[20005];
char a[5];
int father[20005];
int index[20005];
int x,y,repair;
double judge(double x,double y,double z,double w)
{
return sqrt((x-z)*(x-z)+(y-w)*(y-w));
}
int Find(int x)
{
if(x!=father[x])
father[x]=Find(father[x]);
return father[x];
}
void combine(int a,int b)
{
int fa=Find(a);
int fb=Find(b);
if(fa!=fb)
father[fa]=fb;
}
int main()
{
int N,m;
scanf("%d%d",&N,&m);
for(int i=0;i<=N;++i)
{
father[i]=i;
}
for(int i=1;i<=N;++i)
{
scanf("%d%d",&beginn[i].x,&beginn[i].y);
}
while(~scanf("%s",a))//现在这个a[0]为我要判断的执行情况
{
if(a[0]=='S')
{
scanf("%d%d",&x,&y);
if(Find(x)==Find(y))//看看他们的祖先是不是一样的
{
printf("SUCCESS\n");
}
else
{
printf("FAIL\n");
}
}
else//现在代表的是修复
{
scanf("%d",&repair);
index[repair]=1;//这里等于1代表已经修复了
//那么就要寻找能够跟这点建立联系的点了
for(int i=1;i<=N;++i)
{
if(index[i]==0||i==repair) continue;
//pass掉的是没有修复的点和这一点
if(judge(beginn[i].x,beginn[i].y,beginn[repair].x,beginn[repair].y)<=m)//不一定是直线,所以要判断
combine(i,repair);//判断后就是合并
}
}
}
return 0;
}