poj2236(并查集)
来源:互联网 发布:mac 查看安装路径 编辑:程序博客网 时间:2024/06/08 12:14
http://poj.org/problem?id=2236(poj2236 点击打开链接)
思路:先预处理初所有点到其他点的距离,输入’O’的时候更新并查集。
分析:最开始想要先把所有点之间的距离打表,但是不知道之后怎么和并查集联系,就是不知道怎么更新并查集,还是看了别人的思路后写的,进步的是没有看代码,代码完全是自己敲的,开心o( ̄▽ ̄)ブ
代码:
#include <iostream>#include <string>#include <cstring>#include <algorithm>#include <cmath>#include <vector>#include <cstdio>#include <map>#include <cstdlib>#include <deque>#include <iterator>#define pi 2*acos(0)using namespace std;const int maxn = 1001 + 10;struct node{ int x,y;} nodes[maxn];int father[maxn];double dis[maxn][maxn];bool isok[maxn];int n,d;void init(){ for(int i = 0; i <= n; i++) { father[i] = i; } for(int i = 1; i < n; i++) { for(int j = i + 1; j <= n; j++) { dis[i][j] = sqrt((double)(nodes[i].x-nodes[j].x) * (nodes[i].x-nodes[j].x) + (double)(nodes[i].y-nodes[j].y) * (nodes[i].y-nodes[j].y)); dis[j][i] = sqrt((double)(nodes[i].x-nodes[j].x) * (nodes[i].x-nodes[j].x) + (double)(nodes[i].y-nodes[j].y) * (nodes[i].y-nodes[j].y)); } } memset(isok,false,sizeof(isok));//初始化所有的电脑都是坏的。}int find(int x){ if(x == father[x]) return x; int y = find(father[x]); return father[x] = y;}void unin(int a,int b){ int fa = find(a); int fb = find(b); father[fa] = fb;}int main(){ scanf("%d%d",&n,&d); int a,b; for(int i = 1; i <= n; i++) { scanf("%d%d%*c",&nodes[i].x,&nodes[i].y); } init(); int num1,num2; char ch; while(~scanf("%c",&ch)) { if(ch == 'O') { scanf("%d%*c",&num1); isok[num1] = true; //每次修好一台电脑,就遍历所有的电脑,根据距离判断是否可以进行联系,可以的合并两颗树。 for(int i = 1; i <= n; i++) { if(isok[i] && i != num1) { if((dis[num1][i] <= (double)d) || (dis[i][num1] <= (double)d) ) { if(find(num1) != find(i)) { unin(num1,i); } } } } } else if(ch == 'S') { scanf("%d%d%*c",&num1,&num2); if(find(num1) == find(num2))//查询,如果根节点相同,就可以联系。 printf("SUCCESS\n"); else printf("FAIL\n"); } }}
- 有时候坚持一下下,别去看代码,万一自己写出来了呢,是吧。
阅读全文
0 0
- poj2236 并查集
- poj2236并查集
- poj2236 并查集
- poj2236(并查集)
- POJ2236(并查集)
- 并查集-poj2236
- poj2236 并查集
- poj2236 并查集
- poj2236(并查集)
- poj2236 并查集
- POJ2236 并查集
- POJ2236并查集
- POJ2236并查集
- POJ2236-并查集
- poj2236(并查集)
- poj2236(并查集)
- poj2236(并查集)
- POJ2236 Wireless Network(并查集)
- 利用Flask-AppBuilder 快速构建Web后台管理应用
- linux 配置DNS服务器
- 步进电机脱机状态实质
- 【PAT】【Advanced Level】1075. PAT Judge (25)
- python os.path模块
- poj2236(并查集)
- 10分钟学会在windows-Linux下设置ASP.Net Core开发环境并部署应用
- 动态数据曲线绘制
- MVG读书笔记——单应矩阵估计这件小事(一)
- JasperReport| JasperReport Studio操作(下)
- 【安卓学习笔记】JAVA基础Lesson4-函数重载与构造函数
- BZOJ 1003 DP+最短路 解题报告
- Python3之日志模板
- Zend Studio 无法追踪函数的解决方案