POJ 2236 Wireless Network(带权并查集)
来源:互联网 发布:seo黑帽工具有哪些 编辑:程序博客网 时间:2024/06/06 12:49
Wireless Network
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 27063 Accepted: 11205
DescriptionAn earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The computers are repaired one by one, and the network gradually began to work again. Because of the hardware restricts, each computer can only directly communicate with the computers that are not farther than d meters from it. But every computer can be regarded as the intermediary of the communication between two other computers, that is to say computer A and computer B can communicate if computer A and computer B can communicate directly or there is a computer C that can communicate with both A and B.
In the process of repairing the network, workers can take two kinds of operations at every moment, repairing a computer, or testing if two computers can communicate. Your job is to answer all the testing operations.
InputThe first line contains two integers N and d (1 <= N <= 1001, 0 <= d <= 20000). Here N is the number of computers, which are numbered from 1 to N, and D is the maximum distance two computers can communicate directly. In the next N lines, each contains two integers xi, yi (0 <= xi, yi <= 10000), which is the coordinate of N computers. From the (N+1)-th line to the end of input, there are operations, which are carried out one by one. Each line contains an operation in one of following two formats:
1. “O p” (1 <= p <= N), which means repairing computer p.
2. “S p q” (1 <= p, q <= N), which means testing whether computer p and q can communicate.
The input will not exceed 300000 lines.
OutputFor each Testing operation, print “SUCCESS” if the two computers can communicate, or “FAIL” if not.
Sample Input
4 1
0 1
0 2
0 3
0 4
O 1
O 2
O 4
S 1 4
O 3
S 1 4
Sample OutputFAIL
SUCCESS
题意:由n台电脑组成,因地震全部瘫痪,现在进行修复,规定距离小于等于d的电脑修复之后是可以直接相连,进行若干操作,O a,修复编号为a的电脑,S a,b 询问a,b电脑能否联系
题解:并查集
代码:
#include <iostream>#include <cstring>#include <algorithm>#include <map>#include <string>#include <cmath>using namespace std;const int maxn = 20000+5;struct node{ double xi; double yi;}a[maxn];int pre[maxn];//数组pre[]记录了每个点的前导点bool vis[maxn];//用于标记独立块的根结点double dis(int i,int j){ return sqrt((a[i].xi-a[j].xi)*(a[i].xi-a[j].xi)+(a[i].yi-a[j].yi)*(a[i].yi-a[j].yi));}int find(int x){ int r = x; while(r != pre[r]) r = pre[r]; int i = x,j; while(pre[i] != r)//路径压缩 { j = pre[i];// 在改变上级之前用临时变量 j 记录下他的值 pre[i] = r; //把上级改为根节点 i = j; } return r;}void mix(int x,int y){ int fx = find(x); int fy = find(y); if(fx != fy) pre[fy] = fx;}int main(){ int n,d; cin>>n>>d; memset(vis,false,sizeof(vis)); for(int i=1;i<=n;i++) { cin>>a[i].xi>>a[i].yi; pre[i] = i; } string s; int _x,_y; while(cin>>s) { if(s[0] == 'O') { cin>>_x; vis[_x] = true; for(int i=1;i<=n;i++) { if(dis(_x,i) <= d&&vis[i]) mix(i,_x); } } if(s[0] == 'S') { cin>>_x>>_y; if(find(_x) == find(_y)) cout<<"SUCCESS"<<endl; else cout<<"FAIL"<<endl; } } return 0;}
并查集模板:
int pre[maxn];//数组pre[]记录了每个点的前导点 int find(int x){ int r = x; while(r != pre[r]) r = pre[r]; int i = x,j; while(pre[i] != r)//路径压缩 { j = pre[i];// 在改变上级之前用临时变量 j 记录下他的值 pre[i] = r; //把上级改为根节点 i = j; } return r;}void mix(int x,int y){ int fx = find(x); int fy = find(y); if(fx != fy) pre[fy] = fx;}int main(){ ...}
- POJ 2236 Wireless Network(带权并查集)
- 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 2236 Wireless Network (并查集)
- POJ 2236 Wireless Network(并查集)
- poj 2236 Wireless Network 并查集
- Android项目 系统设计架构
- org.apache.ibatis.exceptions.PersistenceException: 前言中没有内容 错误的正确解决方法
- hdu 1394 Minimum Inversion Number -求逆序对的个数- 线段树单节点更新
- Linux系统源代码升级glibc到最新版本2.25.90(正在开发的版本)
- 【spark】使用scala读取项目中的文件的方法
- POJ 2236 Wireless Network(带权并查集)
- MVC流程
- 解决Eclipse中启动Tomcat时出现 Port 8080 required by Tomcat v8.5 Server at localhost is already in use...
- CART
- java配置jdk环境变量的问题
- 【洛谷P3697】开心派对小火车
- freemark
- socket与http的区别
- 面向对象高级之--利用纯面向对象和沙箱模式组织代码