poj 2236【并查集】
2024-10-08 01:11:05
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.
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 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 题意:有一堆坏电脑,每个电脑都有一个二维坐标。有两种操作,修一台电脑和在线询问两台电脑是否可以连通。若两台电脑相距小于等于d或者可以经过第三方电脑连通则连通。给出询问操作的答案。
题解:显然并查集。因为修和询问都是实时在线操作,所以修完一台就看看有没有能和它相连的。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = ; int dx[maxn], dy[maxn], Rank[maxn], par[maxn], re[maxn];
double dis[maxn][maxn];
int N;
double D; void init()
{
for (int i = ; i <= N; i++) {
Rank[i] = ; par[i] = i;
}
} int find(int x)
{
if (par[x] == x) return x;
else return find(par[x]);
} void unite(int xx, int yy)
{
int x = find(xx); int y = find(yy);
if (x == y) return;
if (Rank[x] < Rank[y]) par[x] = y;
else {
par[y] = x;
if (Rank[x] == Rank[y]) Rank[x]++;
}
} bool same(int x, int y)
{
return find(x) == find(y);
} int main()
{
cin >> N;
cin >> D;
init();
for (int i = ; i <= N; i++) cin >> dx[i] >> dy[i];
for(int i=;i<=N;i++)
for (int j = i + ; j <= N; j++) {
dis[i][j] = dis[j][i] = sqrt((double)(dx[i] - dx[j])*(dx[i] - dx[j]) + (double)(dy[i] - dy[j])*(dy[i] - dy[j]));
}
char op; int p, q;
int cnt = ;
while (cin>>op)
{
if (op == 'O') {
cin >> p;
re[cnt++] = p;
for (int i = ; i < cnt - ; i++)
if (dis[re[i]][p] <= D)
unite(re[i], p);
}
else
{
cin >> p >> q;
if (same(p, q))
cout << "SUCCESS" << endl;
else cout << "FAIL" << endl;
}
}
return ;
}
最新文章
- linux中grep的应用
- CSS兼容各浏览器的hack
- Asp.net通过模板(.dot/Html)导出Word,同时导出图片
- UVa 11889 Benefit(数论)
- java 1.7
- [反汇编练习] 160个CrackMe之013
- C++ Dialog Box Command IDs
- JS中==和===的区别
- c# 菜单无限极分类-利用递归
- Windows Phone 独立存储资源管理器工具
- Oracle按不同时间分组统计
- spring boot + neo4j restful
- 浅谈Quartz.Net 从无到有创建实例
- Shell命令-文件及目录操作之chattr、lsattr
- 【hdu 6161】Big binary tree(二叉树、dp)
- Dividing the Path POJ - 2373(单调队列优化dp)
- 01-TCP/IP概述
- Android将Log写入文件
- (转)FFMPEG filter使用实例(实现视频缩放,裁剪,水印等)
- linux分析、诊断及调优的必备“杀器”之一