POJ - 2236Wireless Network-并查集

POJ - 2236

Wireless Network

Time Limit: 10000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit Status

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 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
首先要看懂题目的意思,首先要进行通信,首先你的电脑必须已经修好,否则是不能够通信的,而且通信时距离在一定范围内才能够实现
/*
Author: 2486
Memory: 380 KB		Time: 3063 MS
Language: G++		Result: Accepted
*/
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=10000+5;
int N,d;
char op[10];
int par[maxn],ranks[maxn];
bool vis[maxn];//代表着是否已经修复了电脑,电脑是否正常
struct co {
    int x,y;
} coh[maxn];
void init(int sizes) {
    for(int i=0; i<=sizes; i++) {
        par[i]=i;
        ranks[i]=1;
    }
}
int find(int x) {
    return par[x]==x?x:par[x]=find(par[x]);
}
bool same(int x,int y) {
    return find(x)==find(y);
}
bool judge(int x,int y) {
    return pow((coh[x].x-coh[y].x),2.0)+pow((coh[x].y-coh[y].y),2.0)<=pow(d,2.0);//判断距离是否符合条件
}
void unite(int x,int y) {
    x=find(x);
    y=find(y);
    if(x==y)return ;
    if(ranks[x]>ranks[y]) {
        par[y]=x;
    } else {
        par[x]=y;
        if(ranks[x]==ranks[y])ranks[x]++;
    }
}
int main() {
    int c,e;
    scanf("%d%d",&N,&d);
    init(N);
    memset(vis,false,sizeof(vis));
    for(int i=1; i<=N; i++) {
        scanf("%d%d",&coh[i].x,&coh[i].y);
    }
    while(~scanf("%s",op)) {
        if(op[0]=='O') {
            scanf("%d",&c);
            vis[c]=true;
            for(int i=1; i<=N; i++) {
                if(vis[i]&&judge(c,i)) {//与其他的电脑进行联系的前提是他们是好的,如果不好就连接不上
                    unite(c,i);
                }
            }
        } else {
            scanf("%d%d",&c,&e);
            if(vis[c]&&vis[e]&&same(c,e)) {//是否通信,要看电脑是否已经修复完成
                printf("SUCCESS\n");
            } else printf("FAIL\n");
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-05 10:43:32

POJ - 2236Wireless Network-并查集的相关文章

POJ 2236Wireless Network (并查集)

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

POJ 2492 (简单并查集) A Bug&#39;s Life

题意:有编号为1~n的虫子,开始假设这种昆虫是异性恋.然后已知xi 和 yi进行交配,根据已知情况分析能否推理出其中是否有同性恋 这道题和 POJ 1182 食物链 十分相似,不过在更新与父节点关系的时候要简单一些 sex数组保存的是与父节点的性别关系,如果与父节点是同性,则为0,否则是1 每次路径压缩的同时要更新sex[a] = (sex[a] + sex[temp]) % 2; 还有就是如果x 和 y 不在一个集合,两棵树进行合并的时候,考虑x px y py 四者之间的关系,有 paren

[LA] 3027 - Corporative Network [并查集]

A very big corporation is developing its corporative network. In the beginning each of the N enterprises of the corporation, numerated from 1 to N, organized its own computing and telecommunication center. Soon, for amelioration of the services, the

POJ - Colored Sticks - 并查集+字典树

这道题主要还是要判断是不是欧拉图 说白了就是能不能这幅图能不能用一笔画下来,那么就可以知道了,如果是一个环状的,说明奇数度就不存在,否则就只能用两个奇数度(起点终点)//我的理解这是 只需要用字典树将单词变为对应的一个数字,然后并查集操作就可以,需要维护一个度变量 #include<stdio.h> #include<string.h> int du[500010],p[500010]; int tot=1; struct tree { tree *next[30]; int id

poj 2513 欧拉回路+并查集判断是否联通+Trie树

http://poj.org/problem?id=2513 最初看到 第一感觉---map  一看250000的数据量 果断放弃 然后记得以前看过,trie代替map,尤其当数据量特别大的时候 学到了: 1.Trie代替map的思想,可以在单词结尾的tree[i][tk]  这个i作为字符串对应的int值 ,当然这个int值也可以用于建立并查集 2.接上,通过并查集判断,所有的点在同一个集合图就是联通的,否则不联通,注意tree[i][tk]>0 表示是单词结尾, x=Find(x);//这句

POJ 2236 Wireless Network (并查集)

Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 18066   Accepted: 7618 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computer

poj 2236 Wireless Network (并查集)

链接:http://poj.org/problem?id=2236 题意: 有一个计算机网络,n台计算机全部坏了,给你两种操作: 1.O x 修复第x台计算机 2.S x,y 判断两台计算机是否联通 联通的条件: 两台都修复好了,求距离小于d 思路: 数据很小,我们可以没修复一台就直接枚举已经修复的计算机找到距离小于d的,放到一个并查集里,查询的话直接查询是否再同一个并查集里就好了 实现代码: //while(scanf("\n%c", &ope) != EOF) #inclu

POJ 2236Wireless Network

题目来源:http://poj.org/problem?id=2236 Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16470   Accepted: 6942 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wi

POJ1962:Corporative Network(并查集)

Description A very big corporation is developing its corporative network. In the beginning each of the N enterprises of the corporation, numerated from 1 to N, organized its own computing and telecommunication center. Soon, for amelioration of the se

POJ 1182 食物链 [并查集 带权并查集 开拓思路]

传送门 P - 食物链 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1182 Appoint description:  System Crawler  (2015-01-27) Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物