POJ - 2336 Wireless Network

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

O
O
O
S
O
S
Sample Output FAIL
SUCCESS

题目链接:http://poj.org/problem?id=2236

******************************************

题意:给你n台电脑,然后给出给各电脑坐标,距离不超过d才可以沟通。一开始电脑都是坏的,o操作代表修电脑,s操作判断x与y是否可以联通。

分析:s查询的时候就是简单的并查集,但对于修复更新的时候要注意距离的判断,符合要求的才可以。

一开始FAIL 写成了FALL,唉

 #include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
#include<time.h>
using namespace std; #define N 1550
#define INF 0x3f3f3f3f int Father[N]; struct node
{
int x,y,v;
}a[N];///保存所有的村庄,v标记是否已经修复 int Find(int x)
{
if(x != Father[x])
x=Find(Father[x]);
return x;
} int Len(node a,node b)///求两个村庄的距离
{
int x=b.x-a.x;
int y=b.y-a.y;
return x*x+y*y;
} int main()
{
int n,i,b,c,d;
char s[]; scanf("%d %d", &n,&d);
d=d*d;///因为距离只做判断不做计算,所以直接用平方数判断可以避免小数,更加精确 for(i=;i<=n;i++)
{
scanf("%d %d", &a[i].x,&a[i].y);
a[i].v=;
Father[i]=i;
} while(scanf("%s", s) != EOF)
{
if(s[]=='O')
{
scanf("%d", &b);
if(a[b].v==)
{
a[b].v=;
for(i=;i<=n;i++)
{
if(b!=i&&a[i].v&&Len(a[i],a[b])<=d)
{
int j=Find(i);
int k=Find(b); Father[k]=j;
}
}
} }
else
{
scanf("%d %d", &b,&c);
int j=Find(b);
int k=Find(c);
if(j==k)
printf("SUCCESS\n");
else
printf("FAIL\n");
}
}
return ;
}
上一篇:Javascript ——Navigator对象


下一篇:javascript Navigator对象属性和方法