#2020.01.13训练题解#STL和并查集(E题)

本文探讨了在地震破坏后的无线网络中,如何通过并查集算法修复计算机网络并判断两台计算机间是否能通信的问题。介绍了算法实现过程,包括计算机坐标、通信距离限制及修复与测试操作。

题源POJ-2236

POJ-2236-Wireless Network

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

题意

  • 输入N,d,其中N代表有N台计算机,下面N行是这1-N行计算机的坐标,d是可通信的最大距离
  • 再之后输入O和一个数字,代表修复某台计算机
  • 输入S和两个数字,代表询问这两台计算机能否通信
  • 所有计算机初始都是被损坏待修复的
  • 对于每一次询问输出FAIL和SUCCESS分别代表不可通信和可通信
  • 注意最后的修复和询问有多组数据输入

题解

  • 这题也是一个并查集的基础板子题,套用模板输入输出即可
  • 计算机被损坏待修复可以翻译为各自都是自己的祖先同时flag数组标记未修复
  • 初始化自己是自己的爸爸,flag标记为0
  • 输入的dis[i] [0] 代表第i台电脑的x坐标,dis[i] [1] 代表第i台电脑的y坐标
  • 再输入操作时,判断输入的是O还是S
  • 如果输入的是O的话,要判断是否已经修复,如果没修复就更改flag
  • 然后遍历所有电脑,如果非已经可通信,同时遍历到的电脑已修复
  • 以及它们的距离小于等于最大距离,就将它们放入同一集合
  • 非可通信find函数得到的祖先进行判断(通信的同一祖先)
  • 放入同一集合用baba函数,即使这两台电脑可通信
  • 因为baba函数内部也是先找到祖先再让祖先认爸爸
  • 所以main函数中baba(x,y);和baba(i,a);都可以
  • 如果输入的是S的话,判断这轮输入的两台电脑是否同祖先
  • 同祖先的代表在同一集合,可通信,输出SUCCESS,否则输出FAIL
  • 注意多组输入,输入操作(修复+询问)的时候要while(~scanf("%s",&put))
  • 强调在同一集合内可通信的,必须自己已修复,遍历到的已修复,距离也<=d

涉及知识点

  • 并查集 算法
  • 对于并查集的算法-详见链接博客介绍并查集

AC代码

#include<stdio.h>
#include<string.h>
#include<math.h> 
#include<iostream>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=1010;
int father[maxn],dis[1005][5];
int flag[maxn];
int n=1010;
void init()
{
	for(int i=1;i<=n;i++)
	{
		father[i]=i;
		flag[i]=0;
	}
}
int find(int x)
{
	return x==father[x]?x:father[x]=find(father[x]);
}
void baba(int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	father[fx]=fy;
}
int main()
{
    int m,d,a,b,x,y;
    char put[5];
    scanf("%d %d",&m,&d);
    init();
    for(int i=1;i<=m;i++)
    {
        scanf("%d %d",&dis[i][0],&dis[i][1]);
    }
    while(~scanf("%s",&put))
    {
        if(put[0]=='O')
        {
            scanf("%d",&a);
            if(flag[a]==0)
            {
            	flag[a]=1;
				for(int i=1;i<=n;i++)
				{
					x=find(i);
					y=find(a);
					if(flag[i]&&d*d>=((dis[i][0]-dis[a][0])*(dis[i][0]-dis[a][0])+(dis[i][1]-dis[a][1])*(dis[i][1]-dis[a][1])))
                	{
						if(x!=y)  baba(x,y);//baba(i,a);也可以的
                	}
            	}
			}
        }
        else
        {
            scanf("%d %d",&a,&b);
            if(find(a)==find(b)) printf("SUCCESS\n");
            else printf("FAIL\n");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值