POJ-2236(并查集)
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(并查集)的更多相关文章
- poj 2236 并查集
并查集水题 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring& ...
- Wireless Network POJ - 2236 (并查集)
#include<iostream> #include<vector> #include<string> #include<cmath> #includ ...
- poj 1984 并查集
题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h& ...
- poj 1797(并查集)
http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...
- POJ 2492 并查集扩展(判断同性恋问题)
G - A Bug's Life Time Limit:10000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u S ...
- POJ 2492 并查集应用的扩展
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...
- POJ 3228 [并查集]
题目链接:[http://poj.org/problem?id=3228] 题意:给出n个村庄,每个村庄有金矿和仓库,然后给出m条边连接着这个村子.问题是把所有的金矿都移动到仓库里所要经过的路径的最大 ...
- poj 1733 并查集+hashmap
题意:题目:有一个长度 已知的01串,给出多个条件,[l,r]这个区间中1的个数是奇数还是偶数,问前几个是正确的,没有矛盾 链接:点我 解题思路:hash离散化+并查集 首先我们不考虑离散化:s[x] ...
- poj 3310(并查集判环,图的连通性,树上最长直径路径标记)
题目链接:http://poj.org/problem?id=3310 思路:首先是判断图的连通性,以及是否有环存在,这里我们可以用并查集判断,然后就是找2次dfs找树上最长直径了,并且对树上最长直径 ...
- POJ 3657 并查集
题意: 思路: 1.二分+线段树(但是会TLE 本地测没有任何问题,但是POJ上就是会挂--) 2.二分+并查集 我搞了一下午+一晚上才搞出来----..(多半时间是在查错) 首先 如果我们想知道这头 ...
随机推荐
- HDU 3032 Nim or not Nim?(SG打表找规律)
题意: 给你n堆石子,你每次只能操作一堆石子 1.拿去任意个,最少1个 2.把这一堆分成两堆,没有要求对半分 解析+代码: 1 //解题思路: 2 //对于一个给定的有向无环图,定义关于图的每个顶点的 ...
- ELK Stack 介绍 & Logstash 日志收集
ELK Stack 组成 Software Description Function E:Elasticsearch Java 程序 存储,查询日志 L:Logstash Java 程序 收集.过滤日 ...
- Linux-字符处理命令
目录 1.sort(排序) 2.uniq(不相邻的两行重复不会去除) 3.cut(取列,截取字段) 4.wc(统计行.单词.字符数) 1.sort(排序) 选项: -t # 指定分隔符 -k # 指定 ...
- 一篇文章图文并茂地带你轻松学会 HTML5 storage
html5 storage api localStorage 和 sessionStorage 是 html5 新增的用来存储数据的对象,他们让我们可以以键值对的形式存储信息. 为什么要有 stora ...
- 数据分析常用库(numpy,pandas,matplotlib,scipy)
概述 numpy numpy(numeric python)是 python 的一个开源数值计算库,主要用于数组和矩阵计算.底层是 C 语言,运行效率远高于纯 python 代码.numpy主要包含2 ...
- 【php代码审计】熊海cms1.0
0x01 环境安装 1. 熊海cms1.0 (http://js.down.chinaz.com/201503/xhcms_v1.0.rar) 2.seay代码审计工具 3. phpstudy (ph ...
- openssl的用法
Openssl详细用法: OpenSSL 是一个开源项目,其组成主要包括一下三个组件: openssl:多用途的命令行工具 libcrypto:加密算法库 libssl:加密模块应用库,实现了ssl及 ...
- 1 line of CSS Layouts
1 line of CSS Layouts 10 modern layouts in 1 line of CSS 1. 绝对居中布局 <div class="container&quo ...
- 微信小程序 TypeScript bug
微信小程序 TypeScript bug 执行自定义预览前预处理命令失败! internal/modules/cjs/loader.js:584 throw err; ^ Error: Cannot ...
- CORS OPTIONS
CORS OPTIONS A CORS preflight request is a CORS request that checks to see if the CORS protocol is u ...