POJ-2236 Wireless Network(并查集)

tech2022-07-29  136

Wireless Network

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.

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:

“O p” (1 <= p <= N), which means repairing computer p.“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,m。接下来n行代表损坏基站坐标,m代表你可以到达的距离,然后接下来几行,给你一个字母‘O’或‘S’和 数字,O代表修复第几个点,S代表a和b两个点之间是否通。

这道题就是用并查集来做,就是和往常的并查集不一样,这道题的并查集是在一定距离的,具体看代码。

代码如下:

#include<iostream> #include<algorithm> #include<cmath> using namespace std; const int maxn =1005; double a[maxn];double b[maxn]; int fa[maxn];int n;int h[maxn]; void haoi(){ int i; for(i=1;i<=n;i++){ h[i]=0; } } void init(){ int i; for(i=1;i<=n;i++){ fa[i]=i; } } int find(int x){ return x==fa[x]?x:fa[x]=find(fa[x]); } void baba(int x,int y){ int fx=find(x); int fy=find(y); fa[fx]=fa[fy]; } int main(){ double d;char c;int f; int e;int i;double s1; scanf("%d%lf",&n,&d); init();haoi(); for(i=1;i<=n;i++){ scanf("%lf %lf",&a[i],&b[i]); } getchar(); while(scanf("%c",&c)!=EOF){ if(c=='O'){ scanf("%d",&f);getchar(); h[f]=1; for(i=1;i<=n;i++){ s1=sqrt((a[f]-a[i])*(a[f]-a[i])+(b[f]-b[i])*(b[f]-b[i])); if(s1<=d&&h[i]==1){ baba(f,i); } } } else if(c=='S'){ scanf("%d%d",&f,&e); getchar(); if(find(f)!=find(e))printf("FAIL\n"); else printf("SUCCESS\n"); } }}

道阻且长 自己选的路 跪着也要走完

最新回复(0)