poj 2236 Wireless Network (并查集)

链接:http://poj.org/problem?id=2236

题意:

有一个计算机网络,n台计算机全部坏了,给你两种操作:

1.O x 修复第x台计算机

2.S x,y 判断两台计算机是否联通

联通的条件: 两台都修复好了,求距离小于d

思路:

数据很小,我们可以没修复一台就直接枚举已经修复的计算机找到距离小于d的,放到一个并查集里,查询的话直接查询是否再同一个并查集里就好了

实现代码:

//while(scanf("\n%c", &ope) != EOF)

#include<iostream>
#include<cstdio>
using namespace std;
const int M = 1e4+10;
struct node{
    int x,y,id;
}a[M],b[M];
int n,d;
int f[M];
int find(int x){
    if(x == f[x]) return x;
    return f[x] = find(f[x]);
}

void mix(int x,int y){
    int fx = find(x);
    int fy = find(y);
    if(fx != fy) f[fx] = fy;
}

bool check(node a,node b){
    double num = (a.x - b.x)*(a.x - b.x)+(a.y - b.y)*(a.y - b.y);
    double num1 = d*d*1.0;
    if(num <= num1) return 1;
    return 0;
}

int main()
{
    char op;int k,l,r,cnt = 0;
    scanf("%d%d",&n,&d);
    for(int i = 1;i <= n;i ++)
        scanf("%d%d",&a[i].x,&a[i].y),a[i].id = i;
    for(int i = 0;i <= n;i ++)
        b[i].x = 0,b[i].y=0,b[i].id = 0,f[i]= i;
    while(scanf("\n%c", &op) != EOF){
        if(op == ‘O‘){
            scanf("%d",&k);
            for(int i = 1;i <= cnt;i ++)
                if(check(b[i],a[k]))
                    mix(b[i].id,k);

            b[++cnt].x = a[k].x;b[cnt].y = a[k].y,b[cnt].id = a[k].id;
        }
        else{
            scanf("%d%d",&l,&r);
            if(find(l) == find(r)) printf("SUCCESS\n");
            else printf("FAIL\n");
        }
    }
}

原文地址:https://www.cnblogs.com/kls123/p/9563205.html

时间: 2024-10-24 23:10:48

poj 2236 Wireless Network (并查集)的相关文章

POJ 2236 Wireless Network (并查集)

Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 18066   Accepted: 7618 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computer

POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y均能C通信,则x和y可以通信.现在给出若干个操作, O p 代表修复编号为p的电脑 S p q代表询问p和q是不是能通信. 思路: 并查集即可.. 如果修复了一台电脑,则把与它相连距离不超过d的且修复了的放在一个集合里面. #include<cstdio> #include<cstring&

poj 2236:Wireless Network(并查集,提高题)

Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16065   Accepted: 6778 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computer

[并查集] POJ 2236 Wireless Network

Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 25022   Accepted: 10399 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap compute

POJ - 2236 Wireless Network(简单并查集)

Wireless Network Time Limit: 10000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap co

poj 2236 Wireless Network 【并查集】

Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16832   Accepted: 7068 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computer

poj 2236 Wireless Network(并查集)

题目大意: 给你N台电脑,从1-N.一个数字,表示两台计算机的最大通信距离,超过这个距离就无法进行通信.然后分别告诉这些电脑的坐标,接下来有两种操作,第一种O表示这点电脑修好,第二种S,表示测试这两台电脑能不能进行正常的通信   解题思路: 并查集的简单应用,对每次修好的电脑对其它已经修好的电脑遍历,如果距离小于等于最大通信距离就将他们合并.之后判断2台电脑是不是一个集合中就KO了 1 #pragma comment(linker, "/STACK:1024000000,1024000000&q

POJ 2236 Wireless Network

Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 18146   Accepted: 7660 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 unexpec

poj2236 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