Description
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.
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.
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
并查集=合并函数+查找函数(一般含有路径压缩)
并查集也已经做了好几道题了,但是还是会出现一看就会,一做就短路的情况,所以现在就总结一下
其实在并查集里就是一个路径压缩和树这种数据结构的合体(部分题会掺杂着结构体),浓缩一下这类题目就是很多的不同的元分成若干个组,其中一个元素A和另一个组的某一个元素B有关系,然后就可以得出这个元素A所在的集合也和B所在的集合有关系,从而将两个集合合并起来,在合并的同时也会路径压缩,结合树的概念减少时间复杂度。
并查集的重点就是find()和join()函数,而两个函数的已基本是固定的模式,并且本人一般不爱写join()函数,感觉有点缀余,不过独立出这样的一个函数之后会使代码更加清晰
注意:在做这类题的时候首先要有整体的思路,要不然实现起来会比较困难。
#include<cstdio> #include <iostream> #include<cstring> using namespace std; const int N=1010; bool check[N]; int n,d; struct node { int fa; int x, y; }p[N]; int find(int w) { if(p[w].fa==0) return w; return p[w].fa=find(p[w].fa); } void join(const int i, const int l) { int root1, root2; root1 = find(i); root2 = find(l); if(root1 != root2) if((p[i].x - p[l].x) * (p[i].x - p[l].x) + (p[i].y - p[l].y) * (p[i].y - p[l].y) <= d * d) p[root2].fa = root1; } int main () { int l,w,i; char ch; cin>>n>>d; for(i=1;i<=n;i++) cin>>p[i].x>>p[i].y; memset(check,0,N); while(cin>>ch) { if(ch=='O') { cin>>l; check[l]=1; for(i=1;i<=n;i++) if(check[i]==1&&i!=l) join(i,l); } else { cin>>l>>w; if(find(l)==find(w)) cout<<"SUCCESS"<<endl; else cout<<"FAIL"<<endl; } } return 0; }