【POJ】2236Wireless Network(并差集基础)

Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 41602 Accepted: 17170

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

题目大意:现在给你一个N和D,代表着有N个电脑和限制距离,下面也就是给你的所有的电脑的位置(x,y)然后就是下面的指令了,

其中 “O”+x 代表着我们要激活编号为x的电脑

S+x+y 就是询问了,问你这两个电脑是不是互联的,其中的互联也就是在一个联通块中,

思路:这个题目的话基本上把握好那个限制距离就可以了

我们可以先将某个电脑可以连接的电脑给保存到一个vector路径中,也就是保存能够互联的电脑,虽然有的是不会激活的。

然后我们每每激活一个,我们就可以从这个点的vector中选取那些激活的点并建边,这样就基本上模拟了一个激活后建边的过程,后面的就依此判断就可以了

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#define ll long long
using namespace std;
const int maxn = 1001+5;
double x[maxn],y[maxn];
vector<int >point[maxn];
int vis[maxn];
int F[maxn];
double Get_dist(int i,int j)
{
    return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}
int findset(int x)
{
    if(F[x]==-1)
        return x;
    return F[x]=findset(F[x]);
}
void bind(int i,int j)
{
    int fi=findset(i),fj=findset(j);
    if(fi!=fj)
    {
        F[fj]=fi;
    }
}
int n;
double d;
int main()
{
    memset(F,-1,sizeof(F));
    memset(vis,0,sizeof(vis));
    scanf("%d%lf",&n,&d);
    //cout<<d<<endl;
    for(int i=1; i<=n; i++)
        scanf("%lf%lf",&x[i],&y[i]);

    for(int i=1; i<=n; i++)
        for(int j=i+1; j<=n; j++)
        {
            if(Get_dist(i,j)<=d)
            {
                //cout<<i<<"--"<<j<<endl;
                point[i].push_back(j);
                point[j].push_back(i);
            }
        }
    char ch;
    while(scanf("%s",&ch)!=EOF)
    {
        if(ch=='O')
        {
            int u;
            scanf("%d",&u);
            if(vis[u])
                continue;

            vis[u]=1;
            for(int i=0; i<point[u].size(); i++)
            {
                int v=point[u][i];
                //cout<<v<<endl;
                if(vis[v]==0)
                    continue;
                //cout<<u<<"--"<<v<<endl;
                bind(u,v);
            }
        }
        else if(ch=='S')
        {
            int u,v;
            scanf("%d%d",&u,&v);
            if(vis[u]==0||vis[v]==0)
            {
                printf("FAIL\n");
            }
            else
            {
                int fu=findset(u),fv=findset(v);
                if(fu!=fv)
                    printf("FAIL\n");
                else
                    printf("SUCCESS\n");
            }
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值