Wireless NetWork

POJ-2236

  • 需要注意这里的树的深度需要初始化为0。
  • 而且,find函数需要使用路径压缩,这里的unint合并函数也使用了优化(用一开始简单的合并过不了)。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
int n,d;
bool repaired[1002];
struct node{
int x;
int y;
};
node com[1002];
int set[1002];
int rank1[1002];//rank1[i]表示i的深度
int find(int x){
if(x==set[x])
return set[x];
return set[x]=find(set[x]);
}
void unint(int a,int b){
int ta=find(a);
int tb=find(b);
if(ta!=tb){
if(rank1[ta]<rank1[tb]){
set[ta]=tb;
}else{
set[tb]=ta;
if(rank1[ta]==rank1[tb]){
rank1[ta]++;
}
}
}else return;
}
double compute(int a,int b){
return (com[a].x-com[b].x)*(com[a].x-com[b].x)+(com[a].y-com[b].y)*(com[a].y-com[b].y);
}
int main(){
for(int i=0;i<1002;i++){
set[i]=i;
rank1[i]=0;
}
cin>>n>>d;
int x,y;
for(int i=1;i<=n;i++){
scanf("%d%d",&com[i].x,&com[i].y);
}
char c;
while(scanf("%c",&c)!=EOF){
if(c=='O'){//修复
int num;
scanf("%d",&num);
repaired[num]=true;
for(int i=1;i<=n;i++){
if(i!=num&&repaired[i]&&compute(num,i)<=d*d){
unint(i,num);
}
}
}else if(c=='S'){
int x,y;
scanf("%d%d",&x,&y);
if(repaired[x]&&repaired[y]){
int t1=find(x);
int t2=find(y);
if(t1==t2){
cout<<"SUCCESS"<<endl;
}else cout<<"FAIL"<<endl;
}else{
cout<<"FAIL"<<endl;
}
}
}
return 0;
}

POJ-2236(并查集)的更多相关文章

  1. poj 2236 并查集

    并查集水题 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring& ...

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

    #include<iostream> #include<vector> #include<string> #include<cmath> #includ ...

  3. poj 1984 并查集

    题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h& ...

  4. poj 1797(并查集)

    http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...

  5. POJ 2492 并查集扩展(判断同性恋问题)

    G - A Bug's Life Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  6. POJ 2492 并查集应用的扩展

    A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...

  7. POJ 3228 [并查集]

    题目链接:[http://poj.org/problem?id=3228] 题意:给出n个村庄,每个村庄有金矿和仓库,然后给出m条边连接着这个村子.问题是把所有的金矿都移动到仓库里所要经过的路径的最大 ...

  8. poj 1733 并查集+hashmap

    题意:题目:有一个长度 已知的01串,给出多个条件,[l,r]这个区间中1的个数是奇数还是偶数,问前几个是正确的,没有矛盾 链接:点我 解题思路:hash离散化+并查集 首先我们不考虑离散化:s[x] ...

  9. poj 3310(并查集判环,图的连通性,树上最长直径路径标记)

    题目链接:http://poj.org/problem?id=3310 思路:首先是判断图的连通性,以及是否有环存在,这里我们可以用并查集判断,然后就是找2次dfs找树上最长直径了,并且对树上最长直径 ...

  10. POJ 3657 并查集

    题意: 思路: 1.二分+线段树(但是会TLE 本地测没有任何问题,但是POJ上就是会挂--) 2.二分+并查集 我搞了一下午+一晚上才搞出来----..(多半时间是在查错) 首先 如果我们想知道这头 ...

随机推荐

  1. UVA-257 哈希算法

    UVA-257 题意: 给你很多串,你需要找到这个串内有没有两个长度大于3的回文字符串,且要保证这两个回文字符串不相同,也不能完全覆盖,但可以重合一部分 题解: 首先判断回文的话可以通过马拉车算法(M ...

  2. Codeforces Round #575 (Div. 3) B. Odd Sum Segments 、C Robot Breakout

    传送门 B题题意: 给你n个数,让你把这n个数分成k个段(不能随意调动元素位置).你需要保证这k个段里面所有元素加起来的和是一个奇数.问可不可以这样划分成功.如果可以打印YES,之后打印出来是从哪里开 ...

  3. Codeforces Round #643 (Div. 2) D. Game With Array (思维,构造)

    题意:给你两个正整数\(N\)和\(S\),构造一个长度为\(N\)并且所有元素和为\(S\)的正整数数组,问是否能找到一个\(K (0\le K \le S)\)使得这个数组的任意_子数组_的和都不 ...

  4. Musical Theme POJ - 1743 后缀数组

    A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the ...

  5. Leetcode(28)-实现strStr()

    实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...

  6. Netty(二)Netty 与 NIO 之前世今生

    2.1 Java NIO 三件套 在 NIO 中有几个核心对象需要掌握:缓冲区(Buffer).选择器(Selector).通道(Channel). 2.1.1 缓冲区 Buffer 1.Buffer ...

  7. 2019牛客多校第四场B xor(线性基求交)题解

    题意: 传送门 给\(n\)个集合,每个集合有一些数.给出\(m\)个询问,再给出\(l\)和\(r\)和一个数\(v\),问你任意的\(i \in[l,r]\)的集合,能不能找出子集异或为\(v\) ...

  8. Java 对象的哈希值是每次 hashCode() 方法调用重计算么?

    对于没有覆盖hashCode()方法的对象 如果没有覆盖 hashCode() 方法,那么哈希值为底层 JDK C++ 源码实现,实例每次调用hashcode()方法,只有第一次计算哈希值,之后哈希值 ...

  9. 你所不知道的 JS: null , undefined, NaN, true==1=="1",false==0=="",null== undefined

    1 1 1 === 全相等(全部相等) ==  值相等(部分相等) demo: var x=0; undefined var y=false; undefined if(x===y){ console ...

  10. js console API All In One

    js console API All In One const log = console.log; for(const key in console) { log(`navigator.${key} ...