A - 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的电脑可以通信,给出每个电脑的坐标与修复的电脑标号,询问某两台电脑是否相连。               (距离不够的电脑可以通过中介相连,即c与a相连,c与b相连,则a与b相连)

题解:

先将每台电脑都看成一个集合,每修好一个就试试看能不能和在此之前修好的集合合并(也就是判断两点之间的距离是否小于最大距离),如果能合并就合并,不能就跳过。最后判断某两个点所在集合的标记元素是否一致(即pre[r]是否等于r)。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;

int top, d;
int pre[1050];           //并查集
int lo[1050];           //记录已经修复好的电脑
int rank[1050];         //记录每个点作为集合标记元素的次数,(可以不写)
struct node
{
  double x, y;     //每个点的坐标
}coor[1050];

bool check(int a, int b)
{
  if(pow(coor[a].x - coor[b].x, 2) + pow(coor[a].y - coor[b].y, 2) <= d * d)
   return true;
  else 
   return false;
}

int find(int x)         //查找该集合的标记元素
{
  int r = x;
  while(pre[r] != r)     
  {
    r = pre[r];
  }
  return r;
}

void link(int x, int y)     //合并两个元素所在集合
{
  int a = find(x);
  int b = find(y);
  if(rank[a] > rank[b])
  {
    pre[b] = a;
    rank[a]++;
  }
  else 
  {
    pre[a] = b;
    rank[b]++;
  }
}


int main()
{
  int n;
  double x, y;
  top = 0;
  cin >> n >> d;
  //初始化
  memset(rank, 0, sizeof(rank));
  for(int i = 1; i <= n; i++)
  {
    pre[i] = i;
  }
  //输入数据
  for(int i = 1; i <= n; i++)
  {
    cin >> x >> y; 
    coor[i].x = x;
    coor[i].y = y;
  }
  char ch[6];
  while(~scanf("%s", ch))
  {
    int x, y;
    if(ch[0] == 'O')
    {
      scanf("%d", &x);
      for(int i = 0; i < top; i++)
      {
        if(check(lo[i], x))     //如果距离足够
	{
	  link(lo[i], x);
	}
      }
      lo[top++] = x;
    }
    else
    {
      scanf("%d%d", &x, &y);
      if(find(x) != find(y))
        cout << "FAIL" << endl;
      else 
        cout << "SUCCESS" << endl;
    }
  }
  return 0;
}

猜你喜欢

转载自blog.csdn.net/aqa2037299560/article/details/80697684