POJ—2236 Wireless Network

简介: POJ—2236 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

解题思路:这是一个模拟加并查集的题,题意是一共有n台计算机距离为小于d的可以建立通信,修复计算机之后看看能不能两个计算机之间可以建立通信。对于修复一台电脑可以把该电脑标记,然后去寻找当前计算机与剩余的计算机之间是否满足通信的条件,既该电脑被修复且之间的距离小于d,然后让满足条件的其他电脑认该电脑为祖宗。

程序代码:

#include<stdio.h>
#include<math.h>
int f[50000]={0},n,m,d,a[100100],x[50000],y[50000];
int flag;
void init()
{
  int i;
  for(i=1;i<=n;i++)
    f[i]=i;
  
}
int juli(int p,int q)
{
  int sum;
  return sum=(x[p]-x[q])*(x[p]-x[q])+(y[p]-y[q])*(y[p]-y[q]);
}
int getf(int v)
{
  if(f[v]==v)
    return v;
  else
  {
    f[v]=getf(f[v]);
    return f[v];
  }
} 
void merge(int v)
{
  int t1,t2,sum,j;
  t1=getf(v);
  for(j=1;j<=n;j++)
  {
    if(a[j]==1&&v!=j)
    {
      if(sqrt(juli(v,j)*1.0)<=d)
      { 
        t2=getf(j);
        if(t1!=t2)
        {
          f[t2]=t1;
        //  printf("%d %d\n",t2,t1);
        }
      } 
    }
  } 
}
int main()
{
  int k,t,c,i;
  char s;
  while(scanf("%d%d",&n,&d)!=EOF)
  {
    init();
    for(i=1;i<=n;i++)
      scanf("%d%d",&x[i],&y[i]);
    while(scanf("%c",&s)!=EOF)
    {
      if(s=='O')
      {
        scanf("%d",&m);
        a[m]=1;
        merge(m); 
      }
      if(s=='S')
      {
        scanf("%d%d",&k,&t);
        if(getf(k)==getf(t))
          printf("SUCCESS\n");  
        else
          printf("FAIL\n"); 
      }
    } 
  }
  return 0;
}
相关文章
|
6月前
Arctic Network( POJ - 2349)
Arctic Network( POJ - 2349)
[POJ 1236] Network of Schools | Tarjan缩点
Description A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”).
136 0
[POJ 1236] Network of Schools | Tarjan缩点
|
Ubuntu Linux
Wireless-N 7265
ubuntu linux wifi
|
人工智能
A - Wireless Network
POJ-2236 Time Limit: 10000MS Memory Limit: 65536K Description An earthquake takes place in Southeast Asia.
1024 0
|
算法 数据建模
【POJ 1236 Network of Schools】强联通分量问题 Tarjan算法,缩点
题目链接:http://poj.org/problem?id=1236 题意:给定一个表示n所学校网络连通关系的有向图。现要通过网络分发软件,规则是:若顶点u,v存在通路,发给u,则v可以通过网络从u接收到。
1184 0