POJ-2236 Wireless Network

来源:互联网 发布:淘宝朵以专卖店 编辑:程序博客网 时间:2024/06/13 09:51
Wireless Network
Time Limit: 10000MS Memory Limit: 65536KTotal Submissions: 25451 Accepted: 10574

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 10 10 20 30 4O 1O 2O 4S 1 4O 3S 1 4

Sample Output

FAIL

SUCCESS

题意:有n个需要维修的电脑,在通讯范围内的电脑可以两两联通,根据输入来判断两个电脑是否能连通;

思路:并查集的简单应用,用一个数组标记已维修的电脑,若两个已维修过的电脑距离小于d就合并即可;

下面附上代码:

#include<cstdio>#include<cmath>#include<cstring>#define max 310000int pre[max],x[max],y[max],rank[max]; //负责记录已维修的电脑void AA(int n){int i;for(i=1;i<=n;i++) pre[i]=i;}int find(int x){int r=x;while(r!=pre[r])r=pre[r];//寻根 int i=x,j;while(pre[i]!=r){j=pre[i];pre[i]=r;i=j;}//路径压缩 return r;} void HB(int x,int y){int fx=find(x),fy=find(y);if(fx!=fy) pre[fy]=fx;}int main(){int N,d,p,k,l,d1,t,j;char s[10];while(scanf("%d %d",&N,&d)!=EOF){AA(N);memset(rank,0,sizeof(rank));//设为均未维修 memset(s,0,sizeof(s));for(int i=1;i<=N;i++){scanf("%d %d",&x[i],&y[i]);}while(scanf("%s",s)!=EOF){if(s[0]=='O'){scanf("%d",&p);rank[p]=1;//标记为已维修 for(int i=1;i<=N;i++){d1=(x[p]-x[i])*(x[p]-x[i])+(y[p]-y[i])*(y[p]-y[i]);if(d1<=d*d&&rank[i])//避免使用数学函数,要记好名称 HB(p,i);}}else {scanf("%d %d",&k,&l);if(rank[k]&&rank[l]&&find(k)==find(l)) printf("SUCCESS\n");//若均已维修 else printf("FAIL\n");}}}return 0;}


0 0
原创粉丝点击