HDU-2236-Wireless Network(并查集)

来源:互联网 发布:珠海联邦制药知乎 编辑:程序博客网 时间:2024/06/06 19:47

Wireless Network
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 24404 Accepted: 10167
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
Source

题意:首行给出N和D,代表有N个点,接着N行给出N个点的坐标,接着开头是O代表激活某点,开头是S,代表询问两点之间是否联通。初始任意点都没有联通,对于任意两点都被需要激活,且两点间距离小于等于D,两点才能联通。

维护一个并查集即可

代码

#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<math.h>const int maxn=1005;const int INF=0x3f3f3f3f;int Father[maxn];//并查集数组struct node{    int x;    int y;};node num_1[maxn];//N个点的坐标int num_2[maxn];//存储已激活的点int flag[maxn];//标记i点是否被激活int num;//已激活点的数量int N,D;int Find_father(int x)//查找到x的爹{    if(x!=Father[x])        Father[x]=Find_father(Father[x]);    return Father[x];}bool Judge(node x,node y){    double dis=sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));    if(dis>D)        return false;    return true;}int main(){    scanf("%d%d",&N,&D);    for(int i=0; i<=N; i++)    {        flag[i]=0;//标记i点未被激活        Father[i]=i;//i的爹是自己    }    for(int i=1; i<=N; i++)        scanf("%d%d",&num_1[i].x,&num_1[i].y);    num=0;    char str[2];    while(scanf("%s",str)!=EOF)    {        if(str[0]=='O')        {            int point;            scanf("%d",&point);            if(flag[point]==0)//如果point没被激活过            {                flag[point]=1;                for(int i=0; i<num; i++) //遍历point和之前激活过的点                {                    if(Judge(num_1[num_2[i]],num_1[point]))//两点距离合法                    {                        int flag_x=Find_father(num_2[i]);                        int flag_y=Find_father(point);                        if(flag_x!=flag_y)                            Father[flag_x]=flag_y;                    }                }                num_2[num++]=point;            }        }        else if(str[0]=='S')        {            int x,y;            scanf("%d%d",&x,&y);            if(Find_father(x)==Find_father(y))                printf("SUCCESS\n");            else                printf("FAIL\n");        }    }    return 0;}
0 0
原创粉丝点击