Wireless Network

来源:互联网 发布:微信矩阵的作用 编辑:程序博客网 时间:2024/04/29 21:37

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

题意:

题目大意是说有一些电脑,编号为1到N,现在这些电脑坏了,无法相互连通,我们需要维修,输入首先输入N和d,N表示有多少台电脑,d表示两台已维修好的电脑若它们之间的距离小于等于d,则两台电脑可以互通。接下来输入N行,每行输入a,b两个数,N行中的第i行表示编号为i的电脑的坐标(用来求两台电脑的距离),在接下来的输入各种操作,O a表示编号为a的电脑被维修好了,S a b则表示询问编号为a和b的电脑能不能互通,若能则输出SUCCESS,若不能则输出FAIL。

思路:

首先先来分析样例,一开始所有电脑都是坏的,然后显示O 1操作,代表了编号为1的电脑被修好,发现之前没有修好的电脑,也就表示编号为1的电脑(后面简称1号)没有可以互通的电脑,接着是2号被修好,发现2号与1号的距离为1,所以2号和1号可以互通,所以对2号和1号执行连接(link)操作,接着是4号被修好,发现1号和2号与4号之间的距离都大于d,也就是说没有连接操作,这是有一个S 1 4询问,可以发现1号和4号并不能互通,因此输出FAIL,接下来修好3号,并且进行2号和3号、4号和3号的连接操作,最后的询问发现1号和4号可以通过2号和3号连接,所以输出SUCCESS。
由上面的分析可以清晰地发现这就是个并查集的题,接下来我们来模拟下样例,首先我们定义一个par数组,用来存放每个结点的父亲节点,以自己为父亲的节点就是最终节点,所以par数组的初始状态为:

下标12345par12345


接下来的连接操作是1号和2号,所以par数组修改为:

下标12345par11345


再接着的连接操作是2号和3号,首先我们发现2号节点的par是1,说明2号不是最终节点,所以我们要找到1号,发现1号是最终节点,而3号也是最终节点,所以将3号节点的父节点置为1,如下:

下标12345par11145


看着这个数组,我们只需要判断两个节点的最终节点是否相等,就可以知道两个节点是否互通,例如此时判断1和4号是否互通,此时1号的最终节点是1号,4号的最终节点是4号,所以不想等,所以不互通。
最后被修复的是4号,所以par数组修改如下:

下标12345par11115


此时可见1号和4号是互通的。

这里我们需要考虑个问题,从上表可以看出上面的节点都是接在1号上面的,这样保证了树的均匀(不知道专业术语怎么输,差不多就是均匀的意思吧)。。但是在代码里我们应该如何保证这个均匀呢,对此我们可以设立 一个rank数组,在连接的时候我们可以比较两个节点的rank,我们可以把rank大的节点作为rank小的节点的父亲,若两个rank相等,则成为父亲的那个节点的rank加1。

找最终节点的代码

int find(int i){    while (par[i] != i){        i = par[i];    }    return i;}

连接结点的代码

void Link(int a, int b){    a = find(a);    b = find(b);    if (r[a] < r[b]){        par[a] = b;    }    else{        par[b] = a;        if (r[a] == r[b])            r[a]++;    }}
最后代码
#include<stdio.h>#define MAX 1050struct node{    int x;    int y;}com[MAX];                                                   记录电脑位置int T,D;int pre[MAX],Isok[MAX];void init(int n){    int i;    for(i=1;i<=n;i++){        pre[i]=i;    }}int Yes(node a,node b){                       判断两个电脑距离是否符合    int x1=(a.x-b.x)*(a.x-b.x);    int y1=(a.y-b.y)*(a.y-b.y);    if(x1+y1<=D*D) return 1;    else return 0;}int Find(int x){                                        找根结点    int r=x;    while(r!=pre[r])        r=pre[r];    int i=x,j;    while(i!=r){                                        路径压缩        j=pre[i];        pre[i]=r;        i=j;    }    return r;}void mix(int a,int b){    int fa=Find(a),fb=Find(b);    if(fa!=fb) pre[fb]=fa;}int main(){    scanf("%d%d",&T,&D);    int i;    init(T);    for(i=1;i<=T;i++)        scanf("%d%d",&com[i].x,&com[i].y);    char op[5];    while(scanf("%s",op)!=EOF){        if(op[0]=='O'){            int a;            scanf("%d",&a);            Isok[a]=1;            int j;            for(j=1;j<=T;j++){                if(Isok[j]&&Yes(com[j],com[a])){                    mix(a,j);                }            }        }        else{            int a,b;            scanf("%d%d",&a,&b);            int fa=Find(a),fb=Find(b);            if(fa==fb) printf("SUCCESS\n");            else printf("FAIL\n");        }    }return 0;}



原创粉丝点击