F - Wireless Network 暴力更新+并查集

#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#define _for(i,a,b) for(int i= a;i<b;i++)
using namespace std;
const int N = 1e3+5;
typedef long long ll;
int flag[N],f[N];
int n,d;
struct Node{
    int x,y;
}a[N];
int find( int x ){
    if( f[x]==x ) return x;
    return f[x]=find( f[x] );
}
int merge(int x,int y){
    if( find(x)!= find(y) ){
        f[find(x)] = find(y);
    }
}
inline double dis(int x1,int y1,int x2,int y2){
    return sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>d;
    _for(i,1,n+1) cin>>a[i].x>>a[i].y , f[i]=i;
    char c;
    while(cin>>c){
        int j,k;
        if(c==‘O‘){
            cin>>j;
            flag[j] = 1;
            _for(i,1,n+1){
                if( i == j) continue;
                if(flag[i]  && dis( a[i].x,a[i].y,a[j].x,a[j].y ) <= d ){
                    merge( i,j );
                }
            }
        }else if( c==‘S‘){
            cin>>j>>k;
            if( flag[j] && flag[k] &&find(j)==find(k) ){
                cout<<"SUCCESS"<<endl;
            }
            else cout<<"FAIL"<<endl;
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/SunChuangYu/p/12495109.html

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

F - Wireless Network 暴力更新+并查集的相关文章

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

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 (基础并查集)

http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由于硬件的限制,一台电脑和另一台电脑能够相连当他们之间的距离小于d,或者还有一台电脑当中介,分别与两台电脑相连. 在修复的过程中,工作者会有两种操作,修复电脑和询问电脑a和电脑b是否相连.当询问的时候输出答案. 因为输入数据很大,需要快速判断电脑a和电脑b相连,所以自然想到用并查集. 初始时候 全部电

Codeforces 1166 F. Vicky&#39;s Delivery Service 并查集+set

题意:有n个点,m条边,边有c种颜色,q次操作. 每个边都有一种颜色. 然后操作有两种,一种是再加一条边,另一种是查询能否从x达到y. 移动的限制是,连着走两步必须是同一种颜色,如果走奇数步,最后一步可以是任意颜色. 例子:1-2-3-4-5-6. 这个题颜色种类很多,我是用map<int,vector<int>>来存边. 我们首先可以想到 对于点x同种颜色连着的点都是可以相互移动的,所以我们可以用这种方法将它们用并查集合并,所以我们也可以直接map<int,int>来

[北大机试F]:Wireless Network(并查集)

总时间限制:  10000ms 内存限制:  65536kB 描述 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

UVALive 3027 Corporative Network 带权并查集

                     Corporative Network A very big corporation is developing its corporative network. In the beginning each of the N enterprisesof the corporation, numerated from 1 to N, organized its own computing and telecommunication center.Soon,

GYM 101173 F.Free Figurines(贪心||并查集)

原题链接 题意:俄罗斯套娃,给出一个初始状态和终止状态,问至少需要多少步操作才能实现状态转化 贪心做法如果完全拆掉再重装,答案是p[i]和q[i]中不为0的值的个数.现在要求寻找最小步数,显然要减去一些多余的步数.如果初始的一些链的前端是终止的某一条链的连续的一部分,那么这条链就不用被拆开再连上,这样每一个长度为x的链对答案的贡献就是-2*(x-1),对每条链进行同样的操作之后就是答案 #include <iostream> #include<cstdio> #include<

UVA 3027 Corporative Network 带权并查集、

题意:一个企业要去收购一些公司把,使的每个企业之间互联,刚开始每个公司互相独立 给出n个公司,两种操作 E I:询问I到I它连接点最后一个公司的距离 I I J:将I公司指向J公司,也就是J公司是I公司的上级,距离为abs(I-J)%1000(貌似G++不支持abs,PE了两发) 思路:转化一下题意就行了,首先刚开始的时候每个公司都是独立的,I操作就是并查集中合并操作,将I这课树并到J这个树上, E操作要求的东西就是 I到I的根节点的距离,先看一个没有路径压缩直接暴力的方法把.(本以为不会过的,

F - True Liars 带权并查集

After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ashore on a foggy island. Though he was exhausted and despaired, he was still fortunate to remember a legend of the foggy island, which he had heard