[kuangbin带你飞]专题五 并查集:A - Wireless Network(并查集)

3 篇文章 0 订阅
3 篇文章 0 订阅

链接https://vjudge.net/contest/66964#problem/A

题目
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.

输入
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.

输出
For each Testing operation, print “SUCCESS” if the two computers can communicate, or “FAIL” if not.

题意
现在有N台电脑报废了,它们分别处于(xi,yi)位置。现规定如果两台可用电脑的距离不大于d,则代表两台电脑可以联机工作。
如果A和B联机,B和C联机,则A和C也可以联机工作。

输入第一行两个数字N,d含义如上。
接下来N行每行有两个数字,代表第i台电脑的坐标。
然后会给出多组操作,每一组首先会输入一个字符。

  • 如果是字母‘O’,接下来会输入一个数字,代表中央会派出人员修好这台电脑;
  • 如果是字母‘S’,接下来会输入两个数字u,v,询问u,v是否可以联机工作。

思路
简单的并查集问题。
实际上我用了1个半小时,原因是在我写完之后发现不知为何,我的并查集更新出现了错误。
一个小时之后,我找到了答案:有一个地方的==我写成了=

在这里插入图片描述
直接给代码,伊丽莎白!!!

在这里插入图片描述
代码

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
struct node
{
    int x,y;
} a[1010];
int fa[1010];
bool vis[1010];
int findf(int x)
{
    if(fa[x]==x)   //就是这个地方困扰了我1个多小时,我眼瞎
    {
        return x;
    }
    else
    {
        return fa[x]=findf(fa[x]);
    }
}
void unite(int x,int y)
{
    x=findf(x);
    y=findf(y);
    if(x!=y)
        fa[x]=y;
    else
        return ;
}
int main()
{
    int n,m;
    cin>>n>>m;
    memset(vis,false,sizeof(vis));
    for(int i=1; i<=n; i++)
        scanf("%d%d",&a[i].x,&a[i].y);
    for(int i=1; i<=n; i++)
        fa[i]=i;
    char s;
    int pos;
    while(~scanf(" %c",&s))
    {
        if(s=='O')
        {
            scanf("%d",&pos);
            vis[pos]=true;
            for(int i=1; i<=n; i++)
            {
                if(vis[i]==true&&i!=pos)
                {
                    if((a[pos].x-a[i].x)*(a[pos].x-a[i].x)+(a[pos].y-a[i].y)*(a[pos].y-a[i].y)<=m*m)   //看距离够不够
                    {
                        unite(pos,i);
                    }
                }
            }
        }
        if(s=='S')
        {
            int u,v;
            scanf("%d%d",&u,&v);
            if(findf(u)==findf(v))
                printf("SUCCESS\n");
            else
                printf("FAIL\n");
        }
    }
}

“男人啊,只要记得1就能活下去了。”
这么看来,还要记住双等号/(ㄒoㄒ)/~~

在这里插入图片描述

下次再见~~

并查集是一种常用的数据结构,用于管理一个不相交集合的数据。在并查集中,每个元素都有一个父节点指向它所属的集合的代表元素。通过查找和合并操作,可以判断两个元素是否属于同一个集合,并将它们合并到同一个集合中。 在解决某些问题时,可以使用并查集进行优化。例如,在区间查询中,可以通过优化并查集的查询过程,快速找到第一个符合条件的点。 对于拆边(destroy)操作,一般的并查集无法直接实现这个功能。但是可以通过一个巧妙的方法来解决。首先,记录下所有不会被拆除的边,然后按照逆序处理这些指令。遇到拆边操作时,将该边重新加入并查集中即可。 在实现并查集时,虽然它是一种树形结构,但只需要使用数组就可以实现。可以通过将每个元素的父节点记录在数组中,来表示元素之间的关系。通过路径压缩和按秩合并等优化策略,可以提高并查集的效率。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [「kuangbin专题并查集专题题解](https://blog.csdn.net/weixin_51216553/article/details/121643742)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [并查集(详细解释+完整C语言代码)](https://blog.csdn.net/weixin_54186646/article/details/124477838)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值