Wireless Network POJ
来源:互联网 发布:办公室网络限制玩游戏 编辑:程序博客网 时间:2024/04/30 21:47
题意:给出一些已经损坏的电脑的坐标,电脑修复后可以与k范围以内的正常工作的电脑连接,给出一些操作,判断能不能成功。
分析:并查集的问题,每修复一台电脑就遍历所有,进行连接。(连接的规则是尽量往先修复的连接,规则随意)
收获:刚开始学习并查集,并查集的建立是基于向上查找父亲,同一父亲就是属于同一个集合。
AC:
#include <iostream> #include <string.h> #include <stdio.h> using namespace std; const int N = 1005; struct Point { int x,y; }; Point p[N]; int repaired[N]; int pre[N],rank[N]; int dist(Point A,Point B) { return (A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y); } void Init(int n) { for(int i=1;i<=n;i++) { pre[i] = i; rank[i] = 1; } } int Find(int x) { if(pre[x] != x) pre[x] = Find(pre[x]); return pre[x]; } void Union(int x,int y) { x = Find(x); y = Find(y); if(x == y) return; if(rank[x] >= rank[y]) { pre[y] = x; rank[x] += rank[y]; } else { pre[x] = y; rank[y] += rank[x]; } } int main() { char ch[5]; int n,d,x,y; scanf("%d%d",&n,&d); for(int i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y); int cnt = 0; Init(n); while(~scanf("%s",ch)) { if(ch[0] == 'O') { scanf("%d",&x); for(int i=0;i<cnt;i++) { if(dist(p[repaired[i]],p[x]) <= d*d) Union(repaired[i],x); } repaired[cnt++] = x; } else { scanf("%d%d",&x,&y); x = Find(x); y = Find(y); if(x == y) puts("SUCCESS"); else puts("FAIL"); } } return 0;}
0 0
- poj 2236 Wireless Network
- poj 2236 Wireless Network
- poj 2236 Wireless Network
- POJ 2236 Wireless Network
- POJ-2236 Wireless Network
- poj 2236 Wireless Network
- POJ 2236 - Wireless Network
- poj 2236 Wireless Network
- POJ 2236 Wireless Network
- poj 2236 wireless network
- POJ--2236--Wireless Network
- POJ-2236-Wireless Network
- poj 2236 Wireless Network
- poj 2236 Wireless Network
- POJ 2236Wireless Network
- POJ 2236 Wireless Network
- POJ 2236 Wireless Network
- POJ 2236 Wireless Network
- 操作系统清华向勇陈渝版笔记(七) 进程与线程 PCB TCB 进程挂起 用户线程 内核线程 轻量级进程 僵尸队列
- java ssh框架实现简单的邮箱发送
- 深度神经网络(DNN)的正则化
- 代码重构
- Python3.6安装及引入Requests库
- Wireless Network POJ
- mysql datetime 相关函数
- 帖子备份——【黄轩0303生日贺文】永远的赤子,生日快乐
- Unity3D ScrollView滑动优化
- Learning a Part of C++(for ACM/ICPC) (4) 模板初步
- 浅谈OA系统与Portal门户的区别
- Hadoop Namenode元数据文件 Fsimage、editlog、seen_txid说明
- 入门ssm框架的搭建
- bootstrap常用基本