POJ-2236 Wireless Network(并查集变形)

加油。
To be better.

题目链接:
https://vjudge.net/problem/POJ-2236
题目:
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台计算机,现在你可以做两种操作,修理(O)和检测两台计算机是否连通(S),只有修理好的计算机才能连通。连通有个规则,两台计算机的距离不能超过给定的最大距离D(一开始会给你n台计算机的坐标)。检测的时候输出两台计算机是否能连通。
输入
第一行包含两个整数N和d (1 <= N <= 1001, 0 <= d <= 20000)。
这里N是计算机的数量,从1到N, D是两台计算机可以直接通信的最大距离。
在接下来的N行中,每一行包含两个整数xi, yi (0 <= xi, yi <= 10000),这是N台计算机的坐标。
从(N+1)-第一行到输入端,有一个接一个的操作。
每一行包含以下两种格式之一的操作:
1.
“O p”(1 <= p <= N),表示修复计算机p。
2.
“S p q”(1 <= p, q <= N),表示测试计算机p与q是否能够通信。
输出
对于每个测试操作,如果两台计算机能够通信,则打印“成功”;如果不能通信,则打印“失败”。

大体思路
本质上还是并查集,在并查集的结构体中添加一个flag标记该点是否修复成功,
在输入O的时候一个一个去遍历所有的点,检查该点是否修复,并且两点间的距离是否小于D,如果符合条件,则加入一个集合中!

提示
1.需要路径压缩!压缩后的大小几乎是压缩前的一半!
2.注意失败输出的是"FAIL"不是"FALL"!

代码

#include <iostream>
#include <cmath>
using namespace std;
///开始 并查集
int N,D;

struct node
{
    int x,y,par,rak;//x,y坐标,根节点,轶
    bool flag;//是否修复成功
}p[1010];

void Init()
{//初始化函数
    for(int i=1;i<=N;i++)
    {
        p[i].par=i;
        p[i].rak=0;
        p[i].flag=false;
    }
}
int Find(int x)
{//查找函数
    if(p[x].par==x)
        return x;
    else
        return Find(p[x].par);
}

void Union(int x,int y)
{//合并函数
    x=Find(p[x].par);
    y=Find(p[y].par);
    if(x!=y)
    {
        if(p[x].rak<p[y].rak)
            p[x].par=p[y].par;
        else
        {
            p[y].par=p[x].par;
            if(p[x].rak==p[y].rak)
                p[x].rak++;
        }
    }
}
bool check(node a,node b)
{//检查两点间的距离是否符合题意
    double ans;
    ans=sqrt(1.0*(a.x-b.x)*(a.x-b.x)+1.0*(a.y-b.y)*(a.y-b.y));
    if(ans<=D)
        return true;
    else
        return false;
}
int main()
{
    cin>>N>>D;
    Init();
    for(int i=1;i<=N;i++)
    {
        cin>>p[i].x>>p[i].y;
    }
    char c;
    int t,tc;
    while(cin>>c)
    {

        if(c=='O')
        {
            cin>>t;
            p[t].flag=true;
            for(int i=1;i<=N;i++)
            {
                if(p[i].flag&&i!=t&&check(p[t],p[i]))//检查
                    Union(i,t);
            }
        }
        else
        {
            cin>>t>>tc;
            if(Find(t)==Find(tc))
                cout<<"SUCCESS"<<endl;
            else
                cout<<"FAIL"<<endl;
        }
    }
    return 0;
}

关于算法的模板的一些建议
在所有模板代码里面找一个自己喜欢的代码,仔细看,最好能自己打出来,不能自己打出来也要的熟悉算法的过程,然后在网上看些其他人的代码,观察他们的代码风格,可能有些非常晦涩难懂,那就别看了,找那些看得懂的,观察一下有什么不同,然后想一下这样做有什么好处,好的话就加到自己的代码里,久而久之,就形成了自己的代码模板。


很高兴给你一些启示点个赞再走吧

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值