您的位置:首页 > 其它

POJ - 2236 简单的并查集

2017-04-12 21:33 387 查看
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. Thecomputers 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 regardedas 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. InputThe 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 nextN 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 followingtwo 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. OutputFor 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
题意:一个地方发生了地震,然后有N个电脑,现在要让每电脑是否能联系, 只要A和B直接联系,B和C直接联系,那么A和C就可以联系,但是直接联系只能在指定的距离内进行联系。
思路:用结构体记录下每个点的横纵坐标,然后当每次修复一个电脑时候就去遍历一边看是否能联系,下面是代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
int fa[10000 + 10];
bool com[10000 + 10];
struct computer
{
int x, y;

}computers[10000 + 10];
int ssqrt(int x, int i)
{
return (computers[x].x - computers[i].x)*(computers[x].x - computers[i].x) + (computers[x].y - computers[i].y)*(computers[x].y - computers[i].y);
}
void init()
{
for(int i = 1; i < 10000 + 10; i++)
fa[i] = i;
memset(computers, 0, sizeof(computers));
memset(com, 0, sizeof(com));
}
int getf(int x)
{
if(x == fa[x])
return x;
return fa[x] = getf(fa[x]);
}
void Union(int x, int y)
{
x = getf(x);
y = getf(y);
if(x == y)
return;
fa[y] = x;
}
int main()
{
int n, d;
scanf("%d %d", &n, &d);
init();
for(int i = 1; i <= n; i++)
scanf("%d %d", &computers[i].x, &computers[i].y); getchar();
char s;
while(scanf("\n%c", &s) != EOF)
{

if(s == 'O')
{
int x;
scanf("%d", &x);
for(int i = 1; i <= n; i++) // 遍历是否可以直接联系
{
if(ssqrt(x, i) <= d * d && com[i] == 1 && x != i)
Union(x, i);
}
com[x] = 1;
}
else
{
int x, y;
scanf("%d%d",&x, &y);
x = getf(x);
y = getf(y);//判断是否可以联系
if(x != y)
{
printf("FAIL\n");
}
else
printf("SUCCESS\n");
}

}
return 0;

}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: