ʕ·͡ˑ·ཻ ʕ•̫͡• ʔ•̫͡•ཻʕ•̫͡•ʔ•͓͡•ʔ

题目 - 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

1
2
3
4
5
6
7
8
9
10
11
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

1
2
FAIL
SUCCESS

题目大意:

给定学生总数和学生分组,0号学生得了SARS 问有SARS的嫌疑的有多少人,和0同一组即为有嫌疑。

思路:

TIM图片20191201094621

AC代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
using namespace std;
int a[30005] ,group[30005], num[30005];
int findgroup(int x)//查询每个学生的组长是谁。
{
if(x==group[x]) return x;
else return findgroup(group[x]);
}
void join(int x,int y)//在同一组的学生需要把组长选定。
{
int grox=findgroup(x);
int groy=findgroup(y);
if(grox!=groy)
{
group[grox]=groy;
num[groy]+=num[grox];//每一次合并,都需要吧集合中的元素个数加进去。
}
return ;
}
int main()
{
int n,m,k;
while(~scanf("%d %d",&n,&m))
{
if(n==0&&m==0)
break;
for(int i=0; i<n; i++) //开个数组,给数组赋值,即自己自成一组并为组长。
{
group[i]=i;
num[i]=1;
}
while(m--)//共有m个样例。
{
scanf("%d ",&k);
for(int i=0; i<k; i++)
scanf("%d",&a[i]);
for(int i=0; i<k-1; i++)//把在同一组的几个学生合并在一起。
join(a[i],a[i+1]);//如果组长不同,便把组长选定。
}
int t=findgroup(0);//找到生病的0号学生的组长。
printf("%d\n",num[t]);
}
return 0;
}