A - Wireless Network POJ - 2236

题目大意:有n台坏掉的电脑,给出每台电脑的坐标,然后每次询问输入0(字符) x,表示电脑x恢复正常,输入S x y 询问x和y是否可以联网。只要是x和y的距离小于距离d,那么就可以联网,如果有个中介c使得x到c的距离小于d,y到c的距离小于d,那么x和y也可以联网。

题解:当修复好一台电脑后,然后判断与之前修好的电脑的距离,小于d的话,用并查集连在一起。(没敢这样想,感觉这样会T....)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=2E4+7;
int n,d;
int arrx[N],arry[N],pre[N];
int cnt[N];
int pos=0;
int dis(int a,int b){
    return pow(arrx[a]-arrx[b],2)+pow(arry[a]-arry[b],2)<=d*d;
}
int find(int x){
    return x==pre[x]? x:pre[x]=find(pre[x]);
}
void unite(int x,int y){
    x=find(x);y=find(y);
    pre[x]=y;
}
bool check(int x,int y){
    return find(x)==find(y);
}
int main(){
    cin>>n>>d;
    for(int i=1;i<=n;i++) cin>>arrx[i]>>arry[i];
    char s;
    for(int i=0;i<=n;i++) pre[i]=i;
    while(cin>>s){
        if(s==‘S‘){
            int x,y;
            scanf("%d%d",&x,&y);
            if(check(x,y)) cout<<"SUCCESS"<<endl;
            else cout<<"FAIL"<<endl;
        }
        else {
            int x;cin>>x;
            cnt[++pos]=x;
            for(int i=1;i<pos;i++){
                if(dis(x,cnt[i])){
                    unite(x,cnt[i]);
                }
            }
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Accepting/p/12639365.html

时间: 2024-08-29 19:41:07

A - Wireless Network POJ - 2236的相关文章

Wireless Network (poj 2236 并查集)

Language: Default Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 17602   Accepted: 7418 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network wit

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

链接: http://poj.org/problem?id=2236 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#problem/A 代码: #include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<algorithm> #include<iostream>

Wireless Network POJ 2236

http://poj.org/problem?id=2236 题意:现有一些电脑(编号从 1 - N),在修理好某台电脑并且当这台电脑与其他电脑距离不超过 D 的情况下, 其他电脑可以由这台电脑控制.       有两种操作:一是修理编号为 x 的某台电脑, 而是询问你编号从 p - q 的电脑是否都彼此联通 #include <iostream> #include <cstdio> #include <cmath> #include <algorithm>

A - Wireless Network POJ - 2236-kuangbin带你飞

A - Wireless Network POJ - 2236 Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 50348   Accepted: 20619 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with

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: 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

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