并查集-POJ-2236-Wireless Network

来源:互联网 发布:分析成绩的软件 编辑:程序博客网 时间:2024/06/05 16:13

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 1005struct 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;}
0 0
原创粉丝点击