POJ 2236:Wireless Network(并查集)

时间:2022-05-24 00:35:53
Wireless Network
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 36363   Accepted: 15086

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: 
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

题意:对一些损坏的电脑进行维修,如果两个电脑相连,且距离小于d,则这两个电脑间可以相互通信。第一行输入n和d分别代表电脑的个数和能够通信的距离,接下来n行表示n台电脑的坐标。接下来如果开头输入O(注意是字母O不是数字0),继续输入一个数字p表示维修第p台电脑。如果输入S,继续输入两个数p,q,表示询问这两台电脑间是否可以通信,如果能,输出SUCCESS,如果不能,输出FAIL

就是一道并查集模板题,但是要多加一个对两点间距离的判断,最后输入S后,查询p和q的根节点是否相同,如果相同,则可以通信

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define ll long long
#define INF 0x3f3f3f3f
const int maxn=1e6+10;
int a[maxn];
int flag[maxn];
int d;
struct wzy
{
int x,y;
}p[maxn];
int find(int x)
{
if(x!=a[x])
return find(a[x]);
return a[x];
}
void join(int x,int y)
{
int dx=find(x);
int dy=find(y);
if(dy!=dx)
{
a[dx]=dy;
}
}
int vis(int a,int b)
{
if ((p[a].x-p[b].x)*(p[a].x-p[b].x)+(p[a].y-p[b].y)*(p[a].y-p[b].y)<=d*d)//a,b两点距离如果小于d
return 1;
else
return 0;
}
int main(int argc, char const *argv[])
{
int n;
std::cin>>n>>d;
for(int i=1;i<=n;i++)
{
flag[i]=0;
a[i]=i;
}
for(int i=1;i<=n;i++)
{
std::cin>>p[i].x>>p[i].y;
}
char ch;
int x,y;
getchar();
while(std::cin>>ch)
{
if(ch=='O')
{
std::cin>>x;
flag[x]=1;
for(int i=1;i<=n;i++)
{
if(flag[i]&&vis(i,x)&&i!=x)//判断电脑是否修好,两点距离是否小于d
join(i,a[x]);//合并节点
}
}
else if(ch=='S')
{
std::cin>>x>>y;
//判断x和y的根节点
a[x]=find(x);
a[y]=find(y);
if(a[x]!=a[y])
std::cout<<"FAIL"<<std::endl;
else
std::cout<<"SUCCESS"<<std::endl;
}
}
return 0;
}