POJ 2236 Wireless Network (并查集)
題目鏈接:
http://acm.hust.edu.cn/vjudge/contest/123393#problem/A
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:
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
題意:
給出n個點的坐標,一開始任意兩點均不聯通;
接著給出多個操作:
(定義可達:距離小于d,或者經過多條小于d的邊)
題解:
很明顯的并查集模版題.
距離小于等于d即可合并;
查詢時輸出兩點是否在同一集合.
(不要把FAIL輸出成FALL).
注意:先將與點i距離不超過d的點存起來;
當恢復點i后,枚舉可連接的點,只有兩點都被恢復時才能合并.
代碼:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <queue> #include <map> #include <set> #include <vector> #define LL long long #define eps 1e-8 #define maxn 1200 #define inf 0x3f3f3f3f #define IN freopen("in.txt","r",stdin); using namespace std;int fa[maxn]; int rank[maxn];void init_set() {for(int i=0; i<maxn; i++) {fa[i] = i;rank[i] = 0;} }int find_set(int x) {return fa[x] = (x==fa[x]? x:find_set(fa[x])); }void unit_set(int x, int y) {x = find_set(x);y = find_set(y);if(rank[x] < rank[y]) swap(x, y);fa[y] = x;if(rank[x] == rank[y]) rank[x]++; }LL D; bool dis[maxn][maxn]; LL x[maxn],y[maxn]; bool vis[maxn];int main(int argc, char const *argv[]) {//IN;int n;while(scanf("%d %lld", &n,&D) != EOF){D = D*D;init_set();memset(dis, 0, sizeof(dis));memset(vis, 0, sizeof(vis));for(int i=1; i<=n; i++) scanf("%lld %lld", &x[i],&y[i]);for(int i=1; i<=n; i++) {for(int j=1; j<=n; j++) {if((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]) <= D) {dis[i][j] = dis[j][i] = 1;}}}char c; int x,y;while(scanf("%c",&c) != EOF) {if(c=='O') {scanf("%d", &x);vis[x] = 1;for(int i=1; i<=n; i++) if(dis[x][i] && vis[i])unit_set(i, x);}else if(c=='S') {scanf("%d %d", &x,&y);if(find_set(x) == find_set(y)) puts("SUCCESS");else puts("FAIL");}}}return 0; }轉載于:https://www.cnblogs.com/Sunshine-tcf/p/5699043.html
總結
以上是生活随笔為你收集整理的POJ 2236 Wireless Network (并查集)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C# 条件语句 if else
- 下一篇: Unity学习笔记 - Assets,