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.
翻译:
东南亚发生地震。亚洲合作医疗队(acm)与笔记本电脑建立了无线网络,但意外的余震袭击,网络中的所有电脑都坏了。电脑一台接一台地修好了,网络逐渐恢复了工作。由于硬件的限制,每台计算机只能与距离它不超过d米的计算机直接通信。但是每台计算机都可以看作是其它两台计算机之间通信的中介,也就是说,如果计算机A和计算机B可以直接通信,或者有一台计算机C可以同时与A和B通信,那么计算机A和计算机B就可以通信。
在修复网络的过程中,工人可以在每一时刻进行两种操作,修复一台计算机,或者测试两台计算机是否可以通信。你的工作是回答所有的测试操作。
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.
翻译:
第一行包含两个整数n和d(1<=n<=1001,0<=d<=20000)。这里n是计算机的数量,从1到n,d是两台计算机可以直接通信的最大距离。在接下来的n行中,每行包含两个整数xi,yi(0<=xi,yi<=10000),这是n台计算机的坐标。从第(n+1)行到输入的末尾,都有一个接一个执行的操作。每行包含以下两种格式之一的操作:
1。o p“(1<=p<=n),意思是修复计算机p。
2。s p q“(1<=p,q<=n),这意味着测试计算机p和q是否可以通信。
输入不超过300000行。
output
For each Testing operation, print “SUCCESS” if the two computers can communicate, or “FAIL” if not.
翻译:
对于每个测试操作,如果两台计算机可以通信,则打印“success”,否则打印“fail”。
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<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
int repair[1010]={0};//记录修复情况
int par[1010]; //记录连接情况
int pos[1010][2]; //记录计算机的位置坐标
int n,d,b,c;
char a;
void init() //初始化 初始时每台电脑只与自己连接
{
for(int i=0;i<=n;i++)
par[i]=i;
}
int Find(int x) //查看连接情况 并归属于同一上级
{
if(par[x]!=x)
par[x]=Find(par[x]);
return par[x];
}
void Union(int x,int y) //将两者连接 一方成为另一方的上级
{
par[Find(x)] = Find(y);
}
double dis(int m,int n) //求两点间的距离
{
return sqrt(double(pos[m][0]-pos[n][0])*(pos[m][0]-pos[n][0])+(pos[m][1]-pos[n][1])*(pos[m][1]-pos[n][1]));
}
int main()
{
int i,k=0;
scanf("%d%d",&n,&d); //输入 计算机数量以及最大连接距离
init();
for(i=0;i<n;i++) //输入计算机的坐标
scanf("%d%d",&pos[i][0],&pos[i][1]);
while(scanf("%c",&a)!=EOF) //输入要进行的操作
{
if(a=='O') //修复
{
scanf("%d",&b);
b--;
repair[k]=b;
k++;
for(int j=0;j<k-1;j++) //修复后进行连接
if(repair[j]!=b&&dis(repair[j],b)<=double(d)) //判断能否连接
Union(repair[j],b);
}
if(a=='S') //测试连接
{
scanf("%d%d",&b,&c);
b--,c--;
if(Find(b)==Find(c)) //属于同一上级 即可 连接
printf("SUCCESS\n");
else
printf("FAIL\n");
}
}
return 0;
}