Wireless Network
POJ - 2236
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
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.
Output
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.
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 4Sample Output
FAIL SUCCESS
题意:n个点,d:表示超过这个距离两个计算机之间无法连接(计算机被修理过并且距离小于d才可直接链接),下面n行表示n个点的坐标,O:这个计算机被修理了(开始所有计算机都是坏的),S:查询两个计算机是否可连接
思路:并查集(但是要压缩路径,否则时间太大)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<utility>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#define maxn 1010
#define INF 0x3f3f3f3f
#define LL long long
#define ULL unsigned long long
#define E 1e-8
#define mod 1000000007
#define P pair<int,int>
using namespace std;
int n;
double d;
int par[maxn],rank[maxn],repaired[maxn];
void init()
{
for(int i=0;i<=n;++i){
par[i] = i;
rank[i] = 0;
}
}
int find(int x)
{
if(par[x]==x) return x;
return par[x] = find(par[x]);
}
bool same(int x,int y)
{
if(find(x)==find(y)) return true;
return false;
}
void unite(int x,int y)
{
int fx = find(x);
int fy = find(y);
if(fx == fy) return ;
if(rank[fx]<rank[fy])
par[fx] = fy;
else{
par[fy] = fx;
if(rank[fx]==rank[fy])
rank[fx]++;
}
}
struct Node{
int x,y;
}p[maxn];
double dist(int p1,int p2)
{
return sqrt(1.0*(p[p2].x-p[p1].x)*(p[p2].x-p[p1].x)+1.0*(p[p2].y-p[p1].y)*(p[p2].y-p[p1].y));
}
int main()
{
int p1,p2,px;
char oper;
scanf("%d %lf",&n,&d);
init();
for(int i=1;i<=n;++i){
scanf("%d %d",&p[i].x,&p[i].y);
}
int cnt = 0;
while(scanf("%c",&oper)!=EOF){
if(oper=='O'){
scanf("%d",&px);
for(int i=0;i<cnt;++i){
if(dist(repaired[i],px)<=d)
unite(repaired[i],px);
}
repaired[cnt++] = px;
}
if(oper=='S'){
scanf("%d %d",&p1,&p2);
if(same(p1,p2)){
printf("SUCCESS\n");
}
else{
printf("FAIL\n");
}
}
}
return 0;
}