poj 2236

题意:给你一些电脑,全是坏的。你可以修。他们之间的最长通信距离为d,可以由中间电脑传递信息。

一道简单的并查集,唯一比较麻烦的是需要遍历电脑来看它们能否联通,不过此题给的时间够大。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn = 1001 + 5;
int n, d;
struct node
{
    int x, y;
};
node corr[maxn];
bool vis[maxn];
int fa[maxn];
double dist(int x, int y)//计算距离记得用浮点数
{
    return sqrt((corr[x].x - corr[y].x) * (corr[x].x - corr[y].x) + (corr[x].y - corr[y].y) * (corr[x].y - corr[y].y));
}
void init()
{
    for(int i = 0; i <= n + 1; i++)
        fa[i] = i;
    memset(vis, 0, sizeof(vis));
}
int root(int x)
{
    if(x != fa[x])
        fa[x] = root(fa[x]);
    return fa[x];
}
bool same(int x, int y)
{
    return root(x) == root(y);
}
void unite(int x, int y)
{
    int fx = root(x);
    int fy = root(y);
    fa[fx] = fy;
}
int main()
{
    while(scanf("%d%d", &n, &d) == 2)
    {
        init();
        for(int i = 1; i <= n; i++)
            scanf("%d%d", &corr[i].x, &corr[i].y);
        char cmd[10];
        while(scanf("%s", cmd) == 1)
        {
            if(cmd[0] == 'O')
            {
                int t;
                scanf("%d", &t);
                vis[t] = 1;
                for(int i = 1; i <= n; i++)//遍历查看是否有新的接通
                {
                    //printf("yes\n");
                    if(i != t && vis[i] && !same(i, t) && dist(i, t) <= d)
                    {
                        unite(i, t);
                    }
                }
            }
            else if(cmd[0] == 'S')
            {
                bool flag = false;
                int t1, t2;
                scanf("%d%d", &t1, &t2);
                if(vis[t1] && vis[t2])
                {
                    if(same(t1, t2)) flag = true;
                }
                if(flag) printf("SUCCESS\n");
                else printf("FAIL\n");
            }

        }

    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值