1. 程式人生 > >並查集-POJ-2236-Wireless Network

並查集-POJ-2236-Wireless Network

Wireless Network
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 20573 Accepted: 8648
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臺電腦,給出這n臺電腦的座標,所有電腦的初始狀態全部是斷電的,電腦與電腦之間如果要直接相連,必須是距離相差在最大範圍d以內。現在進行一系列操作,按要求輸出。
操作一共有2種:
1.開啟一臺電腦的電源。
2.查詢兩臺電腦是否能相互聯絡(直接、間接),若能,則輸出SUCCESS,若不能,則輸出FAIL。

題解:
這道題還是一個求是否在同一個連通分量的問題,仍然用並查集。
首先對座標資訊進行處理,依次算出第1~n臺電腦相互的距離,如果不大於d,則加入互相的鄰接表中。
然後開始根據操作處理資料,如果是開啟電源的操作,則將標記為1,並將其加入與之相鄰的開啟的電腦集合中去。如果是查詢操作,則直接檢驗兩臺電腦是否在同一個連通分量中即可。

//
//  main.cpp
//  並查集-A-Wireless Network
//
//  Created by 袁子涵 on 16/1/18.
//  Copyright © 2016年 袁子涵. All rights reserved.
//
//  3313ms  4792kb

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#define MAXN 1005

struct Nd{
    int x,y;
};

using namespace std;

vector<int>dis[MAXN];
Nd nd[MAXN];
int N,d,num1,num2;
bool vis[MAXN];
int pre[MAXN];

double distc(Nd a,Nd b)
{
    double dist;
    dist=sqrt((double)((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)));
    return dist;
}

int find(int num)
{
    int root,now=num,tmp;
    while (pre[now]!=now)
        now=pre[now];
    root=now;
    now=num;
    while (pre[now]!=now) {
        tmp=pre[now];
        pre[now]=root;
        now=tmp;
    }
    return root;
}

void join(int a,int b)
{
    int ra=find(a),rb=find(b);
    if (ra!=rb)
        pre[ra]=rb;
}

int main(int argc, const char * argv[]) {
    scanf("%d%d",&N,&d);
    memset(nd, 0, sizeof(Nd));
    memset(vis, 0, sizeof(vis));
    for (int i=1; i<=N; i++)
    {
        scanf("%d%d",&nd[i].x,&nd[i].y);
        pre[i]=i;
    }
    for (int i=1; i<=N; i++)
        for (int j=i+1; j<=N; j++) {
            double dist=distc(nd[i], nd[j]);
            if (dist<=(double)d) {
                dis[i].push_back(j);
                dis[j].push_back(i);
            }
        }
    char op;
    while (scanf("%c",&op)!=EOF) {
        if (op=='O') {
            scanf("%d",&num1);
            vis[num1]=1;
            for (int i=0; i<dis[num1].size(); i++) {
                if (vis[dis[num1][i]]) {
                    join(dis[num1][i], num1);
                }
            }
        }
        else if(op=='S')
        {
            scanf("%d%d",&num1,&num2);
            if (find(num1)==find(num2))
                printf("SUCCESS\n");
            else
                printf("FAIL\n");
        }
    }
    return 0;
}