poj2236_并查集_Wireless Network
2024-10-13 06:55:09
Wireless Network
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 24497 | Accepted: 10213 |
Description
An 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.
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.
Input
The 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.
The input will not exceed 300000 lines.
Output
For 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 Output
FAIL
SUCCESS
Source
POJ Monthly,HQM
#include <iostream>
#include <cstdio> #define MAX_N 1000+5 using namespace std; struct point{int x;int y;int jud;};
point p[];
int par[MAX_N];//父节点
int depth[MAX_N];//深度 void init(int n){
for(int i=;i<=n;i++){
par[i]=i;
depth[i]=;
}
}
int find_father(int t){
if(t==par[t]){
return t;
}else{
return par[t]=find_father(par[t]);
//实现了路径压缩
}
}
void unite(int t1,int t2){
int f1=find_father(t1);
int f2=find_father(t2);
if(f1==f2){
return ;
}
if(depth[f1]<depth[f2]){
par[f1]=f2;
}else{
par[f2]=f1;
if(depth[f1]==depth[f2]){
depth[f1]++;
//记录深度
}
}
} bool same(int x,int y){
return find_father(x)==find_father(y);
} int distanc_2(point a,point b){
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
} int main()
{
int n,d;
char c;
int t1,t2;
scanf("%d %d",&n,&d);
init(n);
for(int i=;i<=n;i++){
scanf("%d %d",&p[i].x,&p[i].y);
p[i].jud=;
}
getchar();
while(scanf("%c",&c)!=EOF){
if(c=='O'){
scanf("%d",&t1);
getchar();
p[t1].jud=; for(int i=;i<=n;i++){
if(i!=t1&&p[i].jud==){
if(distanc_2(p[t1],p[i])<=d*d){
unite(t1,i);
}
}
}
}else{
scanf("%d %d",&t1,&t2);
getchar();
if(same(t1,t2)){
printf("SUCCESS\n");
}else{
printf("FAIL\n");
}
}
}
return ;
}
最新文章
- DotNet的JSON序列化与反序列化
- 解决VirtualBox只能安装32位系统的问题
- 当call/apply传的第一个参数为null/undefined的时候js函数内执行的上下文对象是什么呢?
- seajs封装js方法
- VUE 入门笔记
- x square x cube
- WPF 获取IP地址
- ruby学习总结01
- ISO中运行时简单使用及KVC补充
- C#实现XML文件数据库存储
- 如何在C#中实现图片缩放
- vector的内存分配与释放
- Weblogic虚拟目录
- iOS 开源一个高度可定制支持各种动画效果,支持单击双击,小红点,支持自定义不规则按钮的tabbar
- FastJson--阿里开源的速度最快的Json和对象转换工具
- AngularJS1.X学习笔记10-自定义指令(下)
- 「ZJOI Day2」游记
- qt无法定位程序输入点 于动态链接库 qt5core.dll
- 二分 poj 3273
- 浅谈sql中的in与not in,exists与not exists的区别以及性能分析