POJ2236并查集

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sumword_/article/details/54847020

Wireless Network
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 25345   Accepted: 10533

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

题目大意:

给你N台电脑,从1-N。一个数字D,表示两台计算机的最大通信距离,超过这个距离就无法进行通信。然后分别告诉这些电脑的坐标,接下来有两种操作,第一种O表示这点电脑修好,第二种S,表示测试这两台电脑能不能进行正常的通信

思路:

    O操作:假如修好了X电脑,那么就遍历所有的电脑I,如果X与I距离小于D且I已经被修好了,就unite(X,I)。

    S操作:检查X与Y的father是否相同,相同就SUCCESS,不同就FAIL

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<math.h>
using namespace std;
#define MAXX 1500
int father[MAXX];
int exist[MAXX];
struct Coordinate{
    int x;
    int y;
}node[MAXX];

int findfather(int num){
    if(father[num] == num)return num;
    else{
        father[num] = findfather(father[num]);
        return father[num];
    }
}
void init(int num){
    for(int i = 0; i < num; i++)
        father[i] = i;
}
void unite(int x, int y){
//cout << x << "   " << y << "!!!" << endl;
    int fx = findfather(x);
    int fy = findfather(y);
    if(fx == fy)return;
    if(fx < fy)father[fy] = fx;
    else father[fx] = fy;
/*for(int i = 1; i <= 4; i++)cout << father[i] << " ";
cout << endl;*/
}

double dist(int a, int b){
    return sqrt(1.0*(node[a].x-node[b].x)*(node[a].x-node[b].x)+1.0*(node[a].y-node[b].y)*(node[a].y-node[b].y));
}

int main(){
    char s[5];
    double d;
    int n,x,y;
    scanf("%d%lf",&n,&d);
    for(int i = 1; i <= n; i++)
        scanf("%d%d",&node[i].x,&node[i].y);
    init(n+10);
    while(scanf("%s",s) != EOF){
        if(*s == 'O'){
            scanf("%d",&x);
            if(exist[x] == 1)continue;
            exist[x] = 1;
            for(int i = 1; i <= n; i++){
                if(exist[i] && dist(x,i) <= d)unite(x,i);
            }
        }
        else if(*s == 'S'){
            scanf("%d%d",&x,&y);
            if(findfather(x) == findfather(y))printf("SUCCESS\n");
            else printf("FAIL\n");
        }
    }
}


猜你喜欢

转载自blog.csdn.net/sumword_/article/details/54847020