POJ 2236 A - Wireless Network[kuangbin带你飞]专题五 并查集

点击打开链接

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

Submit Status Practice POJ
2236

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

题目大意:

一张图上分布着n台坏了的电脑,并知道它们的坐标。两台修好的电脑如果距离<=d就可以联网,也可以通过其他修好的电脑间接相连。给出操作“O x”表示修好x,给出操作“S x y”,请你判断x和y在此时有没有连接上。

解题思路:

这是一个简单的并查集,只是需要判断一下距离是不是满足条件就行了

基本上是模板:

直接贴代码:

/**
2015 - 09 - 18 晚上
Author: ITAK

Motto:

今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
**/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
typedef long long LL;
const int maxn = 1005;
const double eps = 1e-7;

struct node
{
    int x, y;
} arr[maxn];

int rank[maxn], fa[maxn], num[maxn];
///基本上是模板
void Init(int x)
{
    for(int i=1; i<=x; i++)
    {
        fa[i] = i;
        rank[i] = 0;
    }
}

int Find(int x)
{
    if(fa[x] != x)
        fa[x] = Find(fa[x]);
    return fa[x];
}

void Union(int x, int y)
{
    int fx = Find(x), fy = Find(y);
    if(fx == fy)
        return;
    if(rank[fx] > rank[fy])
        fa[fy] = fx;
    else
    {
        fa[fx] = fy;
        if(rank[fx] == rank[fy])
            rank[fy]++;
    }
}
///计算距离(x 与 y)
int Dis(node A, node B)
{
    return (A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y);
}
char str[20];

int main()
{
    int m, d, xx, yy;
    scanf("%d%d",&m, &d);
    Init(m);
    for(int i=1; i<=m; i++)
        scanf("%d%d",&arr[i].x, &arr[i].y);
    int cnt = 0;
    while(cin>>str)
    {
        if(str[0] == 'O')
        {
            cin>>xx;
            for(int i=0; i<cnt; i++)
                ///每次判断是不是需要联合起来
                if(Dis(arr[num[i]], arr[xx]) <= d*d)
                    Union(num[i], xx);
            num[cnt++] = xx;
        }
        else if(str[0] == 'S')
        {
            cin>>xx>>yy;
            int fx = Find(xx);
            int fy = Find(yy);
            if(fx == fy)
                puts("SUCCESS");
            else
                puts("FAIL");
        }
    }
    return 0;
}
时间: 2024-09-28 08:57:38

POJ 2236 A - Wireless Network[kuangbin带你飞]专题五 并查集的相关文章

hdu 3038D - How Many Answers Are Wrong [kuangbin带你飞]专题五 并查集

点击打开链接   2015级新生如何加入ACM集训队?  How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4106    Accepted Submission(s): 1571 Problem Description TT and FF are ... friends. Uh...

hdu 1213 How Many Tables ([kuangbin带你飞]专题五 并查集)

点击打开链接 C - How Many Tables Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1213 Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know

hdu 1272 小希的迷宫[kuangbin带你飞]专题五 并查集

点击打开链接 小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 36540    Accepted Submission(s): 11175 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所

Fire Game [kuangbin带你飞]专题一 简单搜索

点击打开链接 I - Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice FZU 2150 Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the

poj 2236 Wireless Network

点击打开链接poj 2236 思路: 普通并查集 分析: 1 初始值所有的电脑都是坏的,然后给肯定n个点的坐标,只有距离不大于d的才认为连通的 2 现在给定两种操作,O p表示编号为p的电脑被修好了,S p q询问p q是否是连通的 3 简单的并查集,先预处理出所有的点到其他点的距离,然后每一次修好一个电脑之后就去更新并查集,遇到询问的时候只要判断是否在同一个集合里面即可 代码: #include<cmath> #include<cstdio> #include<cstrin

HDU 2818 Building Block, poj 1988 Cube Stacking:带权并查集

链接: HDU: http://acm.hdu.edu.cn/showproblem.php?pid=2818 POJ:  http://poj.org/problem?id=1988 题目: Problem Description John are playing with blocks. There are N blocks (1 <= N <= 30000) numbered 1...N.Initially, there are N piles, and each pile contai

poj 1456 Supermarket:贪心, 并查集

链接: http://poj.org/problem?id=1456 题目: Description A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is measured as an integral number of time units starting from the moment the

poj 1611 The Suspects:并查集

链接: http://poj.org/problem?id=1611 题目: The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 15926   Accepted: 7622 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized a

poj 1703 Find them, Catch them:种类并查集

链接: http://poj.org/problem?id=1703 题目: Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22289   Accepted: 6648 Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up