2016-09-04 15:58 阅读 115 评论 0 推荐 0

POJ 2236 Wireless Network 【并查集】

Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 24091   Accepted: 10022

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

copy
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

copy
FAIL
SUCCESS

Source

POJ Monthly,HQM

题意:有N台坏掉的笔记本电脑,只有修好的并且距离相距不超过d的两台笔记本可以连接,给你每台笔记本的位置和两种操作:

1.“O x”表示修好第x台电脑。

2.“S x y”测试第x台电脑与第y台电脑之间是否联通,是打印“SUCCESS”,否“FAIL”。

思路:并查集,每次解放一个点,并检查是否可以加入集合。

复制代码
copy
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <stack>
#include <cmath>
#include <string>
#include <vector>
#include <cstdlib>
//#include <bits/stdc++.h>
//#define LOACL
#define space " "
using namespace std;
//typedef long long Long;
//typedef __int64 Int;
typedef pair<int, int> paii;
const int INF = 0x3f3f3f3f;
const double ESP = 1e-6;
const double Pi = acos(-1.0);
const int MOD = 1e9 + 7;
const int MAXN = 1e4 + 5;
int par[MAXN], len[MAXN], vis[MAXN];
struct node{
    double x, y;
} loc[MAXN];
void init(int x) {
    for (int i = 0; i <= x; i++) {
        par[i] = i; len[i] = 0; vis[i] = 0;
    }
}
int find_root(int x) {
    if (x == par[x]) return x;
    return par[x] = find_root(par[x]);
}
void unite(int x, int y) {
    int fx = find_root(x);
    int fy = find_root(y);
    if (fx == fy) return;
    if (len[fx] < len[fy]) {
        par[fx] = fy;
    }
    else {
        par[fy] = fx;
        if (len[fx] == len[fy]) len[fx]++;
    }
}
double dis(node a, node b) {
    return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
}
double d;
int n, a, b;
char s[2];
int main() {
    scanf("%d%lf", &n, &d);
    init(n); int cnt = 0;
    for (int i = 1; i <= n; i++) {
        scanf("%lf %lf", &loc[i].x, &loc[i].y);
    }
    while (scanf("%s", s) != EOF) {
        if (s[0] == 'O') {
            scanf("%d", &a);
            for (int i = 0; i < cnt; i++) {
                if (dis(loc[vis[i]], loc[a]) < ESP + d) {
                    unite(a, vis[i]);
                }
            }
            vis[cnt++] = a;
        }
        else {
            scanf("%d%d", &a, &b);
            int tx = find_root(a);
            int ty = find_root(b);
            if (tx == ty) printf("SUCCESS\n");
            else printf("FAIL\n");
        }
    }
    return 0;
}
复制代码

 

copy

 

 

本文作者: zprhhs

本文链接:https://www.cnblogs.com/cniwoq/p/6770800.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   zprhhs  阅读(115)  评论(0编辑  收藏  举报
编辑推荐:
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 数据库服务器 SQL Server 版本升级公告
· C#/.NET/.NET Core技术前沿周刊 | 第 23 期(2025年1.20-1.26)
· 程序员常用高效实用工具推荐,办公效率提升利器!
Power by awescnb
点击右上角即可分享
微信分享提示
进入亮色模式