# [kuangbin带你飞]专题五 并查集：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.

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.

For each Testing operation, print “SUCCESS” if the two computers can communicate, or “FAIL” if not.

• 如果是字母‘O’，接下来会输入一个数字，代表中央会派出人员修好这台电脑；
• 如果是字母‘S’，接下来会输入两个数字u，v，询问u，v是否可以联机工作。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
struct node
{
int x,y;
} a[1010];
int fa[1010];
bool vis[1010];
int findf(int x)
{
if(fa[x]==x)   //就是这个地方困扰了我1个多小时，我眼瞎
{
return x;
}
else
{
return fa[x]=findf(fa[x]);
}
}
void unite(int x,int y)
{
x=findf(x);
y=findf(y);
if(x!=y)
fa[x]=y;
else
return ;
}
int main()
{
int n,m;
cin>>n>>m;
memset(vis,false,sizeof(vis));
for(int i=1; i<=n; i++)
scanf("%d%d",&a[i].x,&a[i].y);
for(int i=1; i<=n; i++)
fa[i]=i;
char s;
int pos;
while(~scanf(" %c",&s))
{
if(s=='O')
{
scanf("%d",&pos);
vis[pos]=true;
for(int i=1; i<=n; i++)
{
if(vis[i]==true&&i!=pos)
{
if((a[pos].x-a[i].x)*(a[pos].x-a[i].x)+(a[pos].y-a[i].y)*(a[pos].y-a[i].y)<=m*m)   //看距离够不够
{
unite(pos,i);
}
}
}
}
if(s=='S')
{
int u,v;
scanf("%d%d",&u,&v);
if(findf(u)==findf(v))
printf("SUCCESS\n");
else
printf("FAIL\n");
}
}
}


“男人啊，只要记得1就能活下去了。”

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 打赏

打赏

想要「替身」的伊丽莎白aru

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 举报
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文

03-04 599

11-01 665
02-23 1875
03-29 1753
01-27 2116
10-25 1398
09-27 239
02-14 177
04-07 671
09-17 1340
03-22 370
05-05 829
01-16 366