POJ 2236

来源:互联网 发布:知乎周刊plus重拾英语 编辑:程序博客网 时间:2024/04/28 17:50

Wireless Network
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 23416 Accepted: 9791
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

题意:告诉你n台电脑的坐标,连续之间的电脑距离为一个单位,和最短的联通距离d,o是修好电脑,s是查看电脑是否联通
题解:在输入的时候就可以预处理很多东西,定义一个数组mark,把电脑的序号存进去,每输入一个就比一下,然后用幷查集联通

#include <stdio.h>#include <string.h>#include <math.h>#define M 100010struct node{    double x, y;};node no[M];bool ok[M];int n, pre[M];double d;int find(int a){    int r = a;    while(r != pre[r])    {        r = pre[r];    }    return r;}void join(int x, int y){    int fx = find(x);    int fy = find(y);    if(fx != fy)    {        pre[fy] = fx;    }}bool dis(int a, int b){    double sum = 0;    sum = sqrt((no[a].x - no[b].x) * (no[a].x - no[b].x) + (no[a].y - no[b].y) * (no[a].y - no[b].y));    if(sum <= d)        return true;    return false;}void init(){    for(int i=1; i<=n; i++)    {        pre[i] = i;    }}int main(){    scanf("%d%lf", &n, &d);    init();    for(int i=1; i<=n; i++)    {        scanf("%lf%lf", &no[i].x, &no[i].y);    }    char ch[2];    int k, a, b, r = 0, mark[M];    getchar();    while(scanf("%s", ch) != EOF)    {        if(ch[0] != 'S')        {            scanf("%d", &k);            for(int i=0; i<r; i++)            {                if(dis(mark[i], k))                    join(mark[i], k);            }            mark[r++] = k;        }        else        {            scanf("%d%d", &a, &b);            if(find(a) == find(b))            {                printf("SUCCESS\n");            }            else            {                printf("FAIL\n");            }        }    }    return 0;}
0 0
原创粉丝点击