简单并查集(1)POJ - 2236
简单并查集
题目:https://vjudge.net/contest/182418#problem/A
A - Wireless Network
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
题意:给定N个通讯点,和距离d
输入N个点分别的坐标
输入O代表修复P点的通讯装置
输入s代表判断x和y两点间是否能通讯
没能输出FAIL
能就输出SUCCESS
思路:
题意很简单,用并查集把能通讯的几个点都联系起来由父节点fa【x】代表,,判断两个点是否属于同一根节点,属于证明可以通讯,否则就不可以。
理解并写出并查集的初始化,找父结点,合并两个并查集等操作。有的题目复杂度过高,需要通过简单操作使查找过程复杂度降低,处理方法为:1,把树深度大的作为根节点(rank小的向rank大的连边)
2,把路径压缩(对于每个节点,一旦走过了一次父节点,就把这个点到父亲的边改为直接连向根)
3,在查询过程中,把经过的所有节点都改为直接连到根上,再次查询的时候就可以很快知道他的根。
代码:虽热和网上代码差不多,那是因为wa了n次之后,无奈看网上代码一步步修改就像了
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<set> #include<algorithm> #include<map> #define maxn 10005 using namespace std; int fa[maxn]; int rank[maxn]; void init(int n) { for(int i=0;i<n;i++) { fa[i]=i; rank[i]=0; } } int find(int x) { if(fa[x]==x)return x; else { return fa[x]=find(fa[x]); } } void unite(int x,int y) { x=find(x); y=find(y); if(x==y)return ; if(rank[x]<rank[y]){ fa[x]=y; } else { fa[y]=x; if(rank[x]==rank[y])rank[x]++; } } bool same(int x,int y) { return find(x)==find(y); } double juli(int x1,int y1,int x2,int y2) { double xx=x1>x2?x1-x2:x2-x1; double yy=y1>y2?y1-y2:y2-y1; return sqrt(xx*xx+yy*yy); } int main() { int n; double d; int x[maxn],y[maxn]; cin>>n>>d; init(n); for(int i=1;i<=n;i++) { cin>>x[i]>>y[i]; } char ch; int d1,d2; int ans[maxn],num=0; memset(ans,0,sizeof(ans)); while(cin>>ch) { if(ch=='O') { cin>>d1; for(int i=0;i<num;i++) { if(juli(x[d1],y[d1],x[ans[i]],y[ans[i]])<=d){ unite(ans[i],d1); } } ans[num++]=d1; } if(ch=='S'){ cin>>d1>>d2; if(find(d1)==find(d2)) { cout<<"SUCCESS"<<endl; } else{ cout<<"FAIL"<<endl; } } } return 0; }
其实在FAIL这个单词这也wa了3发。