POJ 2236 Wireless Network(并查集基础)

这个题难点在于理解题意我觉得,题意理解了就很容易看出是一个并查集的题目来。而且还很基础。

题目:

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台损坏的电脑,现要将其逐台修复,且使其相互恢复通信功能。若两台电脑能相互通信,则有两种情况,一是他们之间的距离小于d,二是他们可以借助都可到达的第三台已修复的电脑。

给出所有电脑的坐标位置,对其进行两种可能的操作,O x表示修复第x台,S x y表示判断x y之间能否通信,若能输出SUCCESS,否则输出FALL。

思路:判断图的连通性问题,可以借助并查集。  首先,电脑之间若能通信,则大前提就是两台电脑都是维修过的,因此处理联通问题时,必须在已修好的电脑中进行。由于通信可以传递,因此只要满足能够通信的条件,则新加入的电脑就可以借助这个小网络与其中任意一台通信,这就是使用并查集的关键条件,因此,将满足距离关系的电脑并入一个集合中,此后每加入一台电脑都与前面加入的所有电脑做一次联通性的判断,即只要距离小于d就并入集合即可。

代码:

#include <stdio.h>
#include <math.h>
#define MAXN 1010
int n, rank[MAXN], father[MAXN], repaired[MAXN];
struct Pos {
    int x, y;
} pos[MAXN];
void make_set()
{
    for (int i = 1; i <= n; ++i)
        rank[i] = 0, father[i] = i;
}
int find_set(int x)
{
    if (x != father[x])  //否则查找集合i的父亲的源头
        father[x] = find_set(father[x]);
    return father[x];  //如果集合i的父亲是自己,说明自己就是源头,返回自己的标号 
}
void union_set(int x, int y)
{
    x = find_set(father[x]);
    y = find_set(father[y]); // 多向上找一层
    if (x != y)
    {
         if (rank[x] < rank[y])//为了保证结合后的树层数尽量的少,保持不变,所以用rank来比较,将短的连接在长的儿子节点上
            father[x] = y;
        else
        {
            father[y] = x;
            if (rank[x] == rank[y])  //相同的话只能加一了
                ++rank[x];
        }
    }
}
double distance(int a, int b) // 注意标号转换一下
{
    Pos x, y;
    x = pos[a];
    y = pos[b];
    return sqrt(1.0*(x.x-y.x)*(x.x-y.x) + 1.0*(x.y-y.y)*(x.y-y.y));
}
int main()
{
    char c;
    int i, x, y, cnt;
    double d;
    scanf("%d%lf", &n, &d);
    for (i = 1; i <= n; ++i) // 编号1到n..
        scanf("%d%d", &pos[i].x, &pos[i].y);
    cnt = 0;
    make_set();
    while (scanf(" %c%d", &c, &x) != EOF)
        if (c == 'O')
        {
            for (i = 0; i < cnt; ++i) // 与已修好的电脑合并
                if (distance(repaired[i], x) <= d)
                    union_set(repaired[i], x);
            repaired[cnt++] = x; // 加入修好的集合中,逻辑其实恰好相反,是先修再合并
        }
        else
        {
             scanf("%d", &y);
             if (find_set(x) == find_set(y)) // 判断连通性
                 printf("SUCCESS\n");
             else
                 printf("FAIL\n");
        }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值