Wireless Network(并查集)

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,计算机之间可通信距离
n行:计算机坐标
n+1之后:o:表示修复计算机
s:表示询问计算机之间能否通讯
思路:
用并查集,开个结构体数组包含计算机损坏情况(是否被修复),坐标,编号(用来找祖宗)
注意限制就行。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct node
{
    int x,y;
    int fg,bk;   //fg:表示当前计算机编号,bk:表示计算机是否损坏(坏0,好1)
} a[1100];
int dis(int i,int j)  //求一下两坐标之间的距离,懒人多做函数._.
{
    return pow((a[i].x-a[j].x),2)+pow((a[i].y-a[j].y),2);
}
int geft(int v)   //查询祖宗
{
    if(a[v].fg==v)
        return v;
    a[v].fg=geft(a[v].fg);
    return a[v].fg;
}
void merge(int v,int u) 
{
    int t1,t2;
    t1=geft(v);
    t2=geft(u);
    if(t1!=t2)
        a[t2].fg=t1;
    return;
}
int main()
{
    char c;
    int n,d,i,j,k;
    while(~scanf("%d%d",&n,&d))
    {
        for(i=1; i<=n; i++)
        {
            a[i].fg=i;   //初始祖宗为它本身
            a[i].bk=0;  //在一开始,计算机都是坏的,不可联通
            scanf("%d%d",&a[i].x,&a[i].y);
        }
        getchar();    //不可或缺~
        while(~scanf("%c",&c))
        {
            if(c=='O')
            {
                scanf("%d",&k);
                a[k].bk=1;      //修复一台就更新一下它和周围的连接情况
                for(j=1; j<=n; j++)
                {
                    if(a[j].bk==0||j==k)
                        continue;
                    if(dis(k,j)<=d*d)
                        merge(k,j);
                }
                for(i=1; i<=n; i++)
                    a[i].fg=geft(a[i].fg);
//                for(i=1;i<=n;i++)
//                    printf("i=%d %d\n",i,a[i].fg);
            }
            if(c=='S')
            {
                int t1,t2;
                scanf("%d%d",&t1,&t2);
                if(a[t1].bk==0||a[t2].bk==0)
                {
                    printf("FAIL\n");
                    continue;
                }
                if(a[t1].fg==a[t2].fg)
                    printf("SUCCESS\n");
                else
                    printf("FAIL\n");
            }
        }
    }
    return 0;
}

上一篇:Sqlserver 在左连接中使用当前查询数据的条件(outer apply)


下一篇:linux中Ctrl+C,Ctrl+Z,Ctrl+D的使用场合