博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj2236 (并查集)
阅读量:6498 次
发布时间:2019-06-24

本文共 2576 字,大约阅读时间需要 8 分钟。

Wireless Network
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 38915   Accepted: 16135

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.

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.

Output

For each Testing operation, print "SUCCESS" if the two computers can communicate, or "FAIL" if not.

Sample Input

4 10 10 20 30 4O 1O 2O 4S 1 4O 3S 1 4

Sample Output

FAILSUCCESS 分析:并查集。
#include
#include
#include
using namespace std;int x[1010],y[1010];int dis[1004][1004];int pre[1010],vis[1010];int N,D;void init(){ for(int i=0;i<=N;i++) pre[i]=i;}int find(int x)//查找点x所在树的根 { int root=x; while(pre[root]!=root) root=pre[root]; int i=x,k; while(k!=root)//路径压缩,让x到根经过的所有节点直接指向root { k=pre[i]; pre[i]=root; i=k; } return root;}void unit(int x,int y){ int rootx=find(x),rooty=find(y); if(rootx!=rooty) { //if(rootx
View Code

 

 

转载于:https://www.cnblogs.com/ACRykl/p/9693484.html

你可能感兴趣的文章
一步步的教你安装UChome (UChome 安装教程)
查看>>
[DeeplearningAI笔记]序列模型1.5-1.6不同类型的循环神经网络/语言模型与序列生成...
查看>>
Kafka深度解析
查看>>
unsigned 后面不跟类型的情况
查看>>
fio硬盘压力测试
查看>>
信号处理——卷积(convolution)的实现
查看>>
多线程同步(循环50 基础加深版)
查看>>
Black and White
查看>>
静态变量和实例变量的区别
查看>>
晨跑【最小费用最大流】
查看>>
景点中心 C组模拟赛
查看>>
bzoj 2733 平衡树启发式合并
查看>>
sublime简书安装配置
查看>>
爱上MVC~Web.Config的Debug和Release版本介绍
查看>>
python__高级 : 动态添加 对象属性, 类属性, 对象实例方法, 类静态方法, 类方法...
查看>>
NLog的介绍使用
查看>>
Haproxy+Rabbitmq中的问题
查看>>
字符串变量小议
查看>>
232. Implement Queue using Stacks
查看>>
Poj(1469),二分图最大匹配
查看>>