题意:
有n台损坏的电脑,现要将其逐台修复,且使其相互恢复通信功能。若两台电脑能相互通信,则有两种情况,一是他们之间的距离小于d,二是他们可以借助都可到达的第三台已修复的电脑。给出所有电脑的坐标位置,对其进行两种可能的操作,O x表示修复第x台,S x y表示判断x y之间能否通信,若能输出SUCCESS,否则输出FALL。

Input:
第一行包含两个整数n和d(1<=n<=1001,0<=d<=20000)。这里n是从1到n的计算机数量,d是两台计算机可以直接通信的最大距离。在n行中,每一个包含两个整数Xi,Yi(0<=Xi,Yi=10000),这是n个计算机的坐标。从(n+1)-第行到输入端,都有一个一个地执行的操作。每行包含以下两种格式之一的操作:
1.“o p”(1<=p<=n),这意味着修复计算机p。
2.“s p q“(1<=p,q<=n),这意味着测试计算机p和q是否可以通信。
输入不超过300000行。

Output:
对于每个测试操作,如果两台计算机可以通信,则打印“成功”,否则打印“失败”。

思路:
并查集:一个等价类的就可以互相通信;每次修好一个电脑,就把它能够通信(已修好且距离小于d)的电脑都与它自己联通


#include<iostream>
#include<string>
#include<cmath>
#include<ctype.h>
#include<memory.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<iomanip>
#include<set>
#include<list>
#include<vector>
#include<stack>
#include<queue>
#define ll long long int
using namespace std;
const int maxn = 1010;
struct jgt
{int x, y;
};
int n, d;
jgt a[maxn];//暂存一下数据
int par[maxn];//父节点
bool is_work[maxn];//记录电脑是否工作的
//vector<int> Map[maxn];//保存图的int MAP[maxn][maxn];int pow2(int x)
{return x * x;
}int find(int x)
{if (par[x] == x)return x;return par[x] = find(par[x]);
}int main()
{cin >> n >> d;for (int i = 1; i <= n; i++){cin >> a[i].x >> a[i].y;par[i] = i;}for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++){if (pow2(a[i].x - a[j].x) + pow2(a[i].y - a[j].y) <= pow2(d)){MAP[i][j] = j;//和后面的查找等价类的时候要对应(类比vectorMAP[j][i] = i;}}char c;while (cin >> c){if (c == 'O'){int num;cin >> num;is_work[num] = true;//for (int i = 0; i < Map[num].size(); i++)//    if (is_work[Map[num][i]])// {//     int f = find(Map[num][i]);//修好的计算机num,与num相连的计算机所属的等价类,都标记成num//     par[f] = num;//    }for (int i = 1; i <= n; i++){if (is_work[MAP[num][i]]){int f = find(MAP[num][i]);par[f] = num;}}}else{int a, b;cin >> a >> b;int fa = find(a);int fb = find(b);if (fa == fb)cout << "SUCCESS" << endl;elsecout << "FAIL" << endl;}}return 0;
}
#include<iostream>
#include<string>
#include<cmath>
#include<ctype.h>
#include<memory.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<iomanip>
#include<set>
#include<list>
#include<vector>
#include<stack>
#include<queue>
#define ll long long int
using namespace std;
const int maxn = 1010;
struct jgt
{int x, y;
};
int n, d;
jgt a[maxn];//暂存一下数据
int par[maxn];//父节点
bool is_work[maxn];//记录电脑是否工作的
vector<int> v[maxn];//保存图的int pow2(int x)
{return x * x;
}int find(int x)
{if (par[x] == x)return x;return par[x] = find(par[x]);
}int main()
{cin >> n >> d;for (int i = 1; i <= n; i++){cin >> a[i].x >> a[i].y;par[i] = i;}/*for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)Map[i][j] = Map[j][i] = sqrt(pow2(a[i].x - a[j].x) + pow2(a[i].y - a[j].y));*/for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++){if (pow2(a[i].x - a[j].x) + pow2(a[i].y - a[j].y) <= pow2(d)){v[i].push_back(j);v[j].push_back(i);}}char c;while (cin >> c){if (c == 'O'){int num;cin >> num;is_work[num] = true;for (int i = 0; i < v[num].size(); i++)if (is_work[v[num][i]]){int f = find(v[num][i]);//修好的计算机num,与num相连的计算机所属的等价类,都标记成numpar[f] = num;}}else{int a, b;cin >> a >> b;int fa = find(a);int fb = find(b);if (fa == fb)cout << "SUCCESS" << endl;elsecout << "FAIL" << endl;}}return 0;
}

poj 2236 WirelessNetwork 并查集相关推荐

  1. A Bug‘s Life POJ 2492 加权并查集

    A Bug's Life POJ 2492 加权并查集 传送门:http://poj.org/problem?id=2492 Description Background Professor Hopp ...

  2. Poj(1703),种类并查集

    题目链接:http://poj.org/problem?id=1703 已经不是第一次接触种类并查集了,直到今天才搞懂. 感谢红黑联盟,感谢杰哥!!! 每个节点只要关系确定,不管是不是同一个集合里面, ...

  3. POJ 1182 食物链 [并查集 带权并查集 开拓思路]

    传送门 P - 食物链 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit  ...

  4. poj 1182 食物链 并查集

    转自一位大佬的最全题解 https://blog.csdn.net/c0de4fun/article/details/7318642 #include <cstdio> #include ...

  5. poj 1703(种类并查集)

    题目大意:在这个城市里有两个黑帮团伙,现在给出N个人,问任意两个人他们是否在同一个团伙 输入D x y代表x于y不在一个团伙里 输入A x y要输出x与y是否在同一团伙或者不确定他们在同一个团伙里 解 ...

  6. poj 1611 TheSuspects 并查集 连通图

    题意: 有一个学校,有N个学生,编号为0-N-1,现在0号学生感染了非典,凡是和0在一个社团的人就会感染,并且这些人如果还参加了别的社团,他所在的社团照样全部感染,求感染的人数. Input: 输入文 ...

  7. C - BLG POJ - 1417 种类并查集加dp(背包)

    思路:刚看这道题感觉什么都不清楚,人物之间的关系一点也看不出来,都不知道怎么写,连并查集都没看出来,但是你可以仔细分析一下,当输入字符串为"yes"的时候,我们设输入的值为x和y, ...

  8. POJ 1611(基础并查集)

    The Suspects 问题分析 基础并查集,就是加了个人数统计. #include <cstdio>using namespace std;const int N = 3e4+3; i ...

  9. P1892 [BOI2003]团伙 +食物链 POJ - 1182 (并查集+思维)

    思路①: 开数组enem[i]记录节点i的敌对节点,当再次输入i的敌对节点时就把他所在并查集的根节点和enem[i]并起来. #include<bits/stdc++.h> using n ...

最新文章

  1. 重磅!PyTorch 中文手册已开源!理论、实践、应用都有了!
  2. Docker 方式安装 gitlab ( 阿里云ECS )
  3. Java开发中如何用JDBC连接起数据库?
  4. C语言和我的世界指令哪个难,我的世界难度有什么区别 难度选择指令介绍
  5. impdp时报错ORA-39082的原因
  6. jdbc 执行oracle命令,JDBC操作oracle
  7. ajax后台如何把对象转为json_SM后台数据与前端(PC、M站、Android、IOS)的交互——页面渲染...
  8. 八年级计算机考操作试题,八年级计算机会考必看试题!!!!!!
  9. 文件完整性的检测与监控
  10. quartus联合仿真步骤
  11. 特征选择 | MATLAB实现特征变量相关性系数图和显著性检验
  12. 你所学的专业是怎么回事——摄影测量与遥感
  13. 【无标题】2021年烷基化工艺证考试及烷基化工艺操作证考试
  14. 470万条12306用户信息遭泄露
  15. 苹果Mac 无法读写NTFS格式的U盘或移动硬盘?一次解决
  16. 微信公告号 图灵机器人实现智能回复
  17. java项目如何发送邮件
  18. Bucket、Hash Chain List
  19. python中用plt画图
  20. gStore:A Graph-based SPARQL Query Engine---part1

热门文章

  1. 1. K近邻算法(KNN)
  2. spring事务源码解析
  3. css :after和:before
  4. i5+GT730+B85安装OSX10.10.5 (Yosemite Install(14F27).cdr)
  5. const指针和指向const对象的指针
  6. 基于visual Studio2013解决C语言竞赛题之1083人机博弈
  7. 数据结构—链表-双链表
  8. 【连载】如何掌握openGauss数据库核心技术?秘诀三:拿捏存储技术(1)
  9. ACDU活动回顾:@DBA,前辈指路不迷茫
  10. 守得云开见月明:一次ASM存储高可用故障解决过程分析