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.二分+并查集 我搞了一下午+一晚上才搞出来----..(多半时间是在查错) 首先 如果我们想知道这头 ...
随机推荐
- c语言实现--单向循环链表操作
1,什么叫单向循环链表.单向循环链表是指在单链表的基础上,表的最后一个元素指向链表头结点,不再是为空. 2,由图可知,单向循环链表的判断条件不再是表为空了,而变成了是否到表头. 3,链表的结点表示 1 ...
- Find a multiple POJ - 2356
The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers ...
- Codeforces Round #649 (Div. 2) C. Ehab and Prefix MEXs (构造,贪心)
题意:有长度为\(n\)的数组\(a\),要求构造一个相同长度的数组\(b\),使得\({b_{1},b_{2},....b_{i}}\)集合中没有出现过的最小的数是\(a_{i}\). 题解:完全可 ...
- 浅谈Webpack模块打包工具四
Webpack 生产环境优化 生产环境和开发环境有很大的差异,生产环境只注重运行效率,开发环境主要开发效率,webpack4.0开始提出了(mode)模式的概念 针对不同的环境进行不同的配置,为不同的 ...
- 关于st表的推导
#include <bits/stdc++.h> using namespace std; const int maxn=1e6+7; int st[maxn][32]; int a[ma ...
- μC/OS-III---I笔记8---事件标志
当任务需要同步时可以使用信号量.A任务给B任务发送消息后B任务才能继续运行.如果需要A任务给任务B传递数据的时候就可以采用消息队列.但对于繁杂任务的同步,比如多个时间发生以后执行一个事件,或者是C任务 ...
- why 2020 you should create a new modern website with web fullstack
why 2020 you should create a new modern website with web fullstack Full-Stack Web Development Front- ...
- ThoughtWorks Homework
ThoughtWorks Homework Homework 考察知识点 项目搭建 技术选型 测试 编码风格 代码质量 设计模式 数据结构 算法 架构 开源协作 CI/CD DevOps Linux ...
- frontends tools
frontends tools figma free online UI 可视化埋点 https://www.figma.com/files/recent cypress testing $ npm ...
- http methods & restful api methods
http methods & restful api methods 超文本传输协议(HTTP)是用于传输超媒体文档(例如HTML)的应用层协议 https://developer.moz ...