E - Wireless Network(并查集)
Input
Output
Sample Input
Sample Output
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.
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.
For each Testing operation, print "SUCCESS" if the two computers can communicate, or "FAIL" if not.
4 1 0 1 0 2 0 3 0 4 O 1 O 2 O 4 S 1 4 O 3 S 1 4
FAIL SUCCESS
题目需要好好理解下,主要的要注意的就是,嗯这两台电脑能被连接 首先它得被修好,其次就是他们之间的距离要小于等于d大体就是这样子
代码:
//我理解错题目意思了应该说只有在被修好的情况下 才可以开始说他们相连难怪无从下手
//前提条件是该电脑已经被修好了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
int n,d;
int pre[1010];
int posx[1010],posy[1010];
double dis[1010][1010];
bool flag[1010];
void init(int n)
{
for(int i=1;i<=n;i++)
pre[i]=i;
}
int find(int r)
{
if(r==pre[r])
return r;
else return pre[r]=find(pre[r]);
}
void merge(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx==fy)return;
else
pre[fx]=fy;
}
int main()
{
scanf("%d%d",&n,&d);
char str[200];
for(int i=1;i<=n;i++)
{
scanf("%d%d",&posx[i],&posy[i]);
}//把他们的位置都输进去 那个i表示的就是哪个点;
int k=0;
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
dis[i][j]=dis[j][i]=sqrt((posx[j]-posx[i])*(posx[j]-posx[i])+(posy[j]-posy[i])*(posy[j]-posy[i]));
}
}//这是他们的距离;
init(n);
//先进行一波操作
int x,y;
while(scanf("%s",str)!=EOF)
{
if(str[0]=='O')
{
scanf("%d",&x);
if(flag[x])continue;
flag[x]=true;
for(int i=1;i<=n;i++)
{
if(flag[i]&&dis[x][i]<=d)
{
merge(x,i);
}
}
}else if(str[0]=='S')
{
scanf("%d%d",&x,&y);
if(find(x)==find(y))
printf("SUCCESS\n");
else printf("FAIL\n");
}
}
return 0;
}