POJ-2236 Wireless Network

Wireless Network
原题链接
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 33631 Accepted: 13981
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
Source

POJ Monthly,HQM

无线网络
[原题链接](http://poj.org/problem?id=2236
时间限制:10000MS内存限制:65536K
提交总数:33631接受:13981
描述

东南亚发生地震。 ACM(亚洲合作医疗团队)已经与膝上型计算机建立了无线网络,但发生了意外的余震袭击,网络中的所有计算机全部断开。电脑被逐一修复,网络逐渐重新开始工作。由于硬件限制,每台计算机只能直接与距离它不远的计算机进行通信。但是,每台计算机都可以看作是另外两台计算机之间通信的中介,也就是说,如果计算机A和计算机B可以直接通信,或者计算机C可以同时与A和B通信B.

在修复网络的过程中,员工可以随时进行两种操作,修复计算机或测试两台计算机是否可以通信。你的工作是回答所有的测试操作。
输入

第一行包含两个整数N和d(1 <= N <= 1001,0 <= d <= 20000)。这里N是计算机的数量,从1到N编号,D是两台计算机可以直接通信的最大距离。在接下来的N行中,每行包含两个整数xi,yi(0 <= xi,yi <= 10000),它是N台计算机的坐标。从第(N + 1)行开始到输入结束,都有一个个执行的操作。每行包含以下两种格式之一的操作:
1.“O p”(1 <= p <= N),这意味着修理计算机p。
2.“S p q”(1 <= p,q <= N),这意味着测试计算机p和q是否可以通信。

输入不会超过300000行。
输出

对于每个测试操作,如果两台计算机可以通信,则输出“SUCCESS”,否则输出“FAIL”。
示例输入

4 1
0 1
0 2
0 3
0 4
O 1
O 2
O 4
S 1 4
O 3
S 1 4
示例输出

FAIL
SUCCESS
资源

POJ Monthly,HQM

题解
典型的并查集。每次修好了就查找有没有修好且距离达到要求的点,然后修正fa数组。

代码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1005;
int x[maxn],y[maxn],n,d,fa[maxn];
bool vis[maxn][maxn],xiuli[maxn];
int pd()
{
    char ch=getchar();
    while (ch!='O'&&ch!='S'&&ch!=EOF) ch=getchar();
    if (ch=='O') return 1;else
    if (ch=='S') return 2;else
    return 0;
}
int read()
{
    int ret=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
    return ret*f;
}
int get(int x){return x==fa[x]? x:fa[x]=get(fa[x]);}
long long sqr(int x){return (long long)x*x;}
int main()
{
    n=read(),d=read();
    for (int i=1;i<=n;i++)
    {
        x[i]=read(),y[i]=read();fa[i]=i;
        for (int j=1;j<i;j++)
          vis[i][j]=vis[j][i]=(sqr(x[i]-x[j])+sqr(y[i]-y[j])<=sqr(d));
    }
    int P=pd();
    while (P)
    {
        if (P==1)
        {
            int x=read();xiuli[x]=1;
            for (int i=1;i<=n;i++)
              if (vis[x][i]&&xiuli[i]) fa[get(i)]=get(x);
        }else
        {
            if (get(read())==get(read()))
              printf("SUCCESS\n");else printf("FAIL\n");
        }
        P=pd();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值