POJ -2236 Wireless Network

题目:

Wireless Network

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

 

题意:给你一些电脑的坐标,每台电脑都有两种状态,修好和未修好,但是两个电脑可以联通只有当电脑都是修好的且两个电脑的祖先相同,其实也就是并查集的应用,,每次修好一台电脑就遍历一遍,把所有修好的电脑且满足距离要求的合并起来, 我也是刚刚开始学习并查集,看了《啊哈算法》的例题再做这道题还是花了不少时间的

遇到的问题:1、最好用scanf输入而不用cin,这个题目应该不至于,别的题好几次用用cin就超时,scanf就AC。

                    2、最后判断success输出的时候要判断getf(x) and getf(Y),不能直接判断f【x】和f【y】,因为合并的时候树的底层可能值不会变(画个图就能理解)

 

 

AC代码:

#include<stdio.h>
int f[1005],n;   
double d;
int dx[1005],dy[1005]; //用来存储每个点的坐标
int rep[1005]; //修好用1表示,未修好用0表示 。全局变量默认全赋值0
double dis(int a,int b) //距离,注意用double类型避免出错
{
    return (dx[a]-dx[b])*(dx[a]-dx[b])+(dy[a]-dy[b])*(dy[a]-dy[b]);
}


void init() //初始化

{
    for(int i=1;i<=n;i++)
        f[i]=i;        
    return;
}

int getf(int x) //压缩路径
{    
    if(x==f[x]) return x;
    else {
        f[x]=getf(f[x]);
        return f[x];
     }
}


void merge(int u,int v)

{
    int t1,t2;
    t1=getf(u);
    t2=getf(v);
    if(t1!=t2)
    {
       f[t2]=t1; //与左边对
    }
}

int main()
{
    int i;
    char ch[10];           // 用字符串的原因是避免 换行符 对下一个要读的字符产生干扰
    int p,q;
    scanf("%d%d",&n,&d); //n为总电脑数,d为可以通信的最短距离
    init();
    for(i=1;i<=n;i++)
        scanf("%d%d",&dx[i],&dy[i]);
    while(scanf("%s",ch)!=EOF)
    {
        if(ch[0]=='O'){
            scanf("%d",&p);
            rep[p]=1;   //表示第p台电脑已经修好
            for(i=1;i<=n;i++)
            {
               if(rep[i]&&dis(i,p)<=d*d) //只有修好的电脑并且距离可以通信才合并
                merge(i,p);
                }    
        }
        else
        {

            scanf("%d%d",&p,&q);
            if(getf(p)==getf(q)&&rep[p]&&rep[q]) printf("SUCCESS\n");
            else printf("FAIL\n");
        }
    }
    return 0;

}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值