POJ 2236 (简单并查集) Wireless Network

题意:

有n个电脑坏掉了,分别给出他们的坐标

有两种操作,可以O x表示修好第x台电脑,可以 S x y表示x y是否连通

两台电脑的距离不超过d便可连通,两台电脑是连通的可以直接连通也可以间接通过第三台电脑连通

思路:

每次修好一台电脑都和前面已经修好的电脑比较一下如果距离小于d而且不在同一网络,便合并在一起即可

 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6
 7 const int maxn = 1000 + 10;
 8 struct Pos
 9 {
10     int x, y;
11 }pos[maxn];
12
13 int p[maxn], o[maxn];
14
15 int Find(int a)
16 {
17     return p[a] == a ? a : p[a] = Find(p[a]);
18 }
19
20 void Union(int a, int b)
21 {
22     int pa = Find(a);
23     int pb = Find(b);
24     if(pa != pb)
25         p[pa] = pb;
26 }
27
28 int main(void)
29 {
30     #ifdef LOCAL
31         freopen("2236in.txt", "r", stdin);
32     #endif
33
34     int n, d, a, b, cnt = 0;
35     char cmd[10];
36     scanf("%d%d", &n, &d);
37     for(int i = 0; i <= n; ++i)    p[i] = i;
38     memset(o, false, sizeof(o));
39     for(int i = 1; i <= n; ++i)
40         scanf("%d%d", &pos[i].x, &pos[i].y);
41     while(scanf("%s", cmd) != EOF)
42     {
43         if(cmd[0] == ‘O‘)
44         {
45             scanf("%d", &a);
46             o[cnt++] = a;
47             for(int i = 0; i < cnt - 1; ++i)
48             {
49                 if((pos[a].x-pos[o[i]].x)*(pos[a].x-pos[o[i]].x) + (pos[a].y-pos[o[i]].y)*(pos[a].y-pos[o[i]].y) <= d * d)
50                 {
51                     Union(a, o[i]);
52                 }
53             }
54         }
55         else
56         {
57             scanf("%d%d", &a, &b);
58             if(Find(a) == Find(b))
59                 puts("SUCCESS");
60             else
61                 puts("FAIL");
62         }
63     }
64
65     return 0;
66 }

代码君

时间: 2024-10-27 18:06:07

POJ 2236 (简单并查集) Wireless Network的相关文章

POJ 2492 (简单并查集) A Bug&#39;s Life

题意:有编号为1~n的虫子,开始假设这种昆虫是异性恋.然后已知xi 和 yi进行交配,根据已知情况分析能否推理出其中是否有同性恋 这道题和 POJ 1182 食物链 十分相似,不过在更新与父节点关系的时候要简单一些 sex数组保存的是与父节点的性别关系,如果与父节点是同性,则为0,否则是1 每次路径压缩的同时要更新sex[a] = (sex[a] + sex[temp]) % 2; 还有就是如果x 和 y 不在一个集合,两棵树进行合并的时候,考虑x px y py 四者之间的关系,有 paren

poj 2236(并查集的应用)

Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 35832   Accepted: 14863 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap compute

POJ - 2236 Wireless Network(简单并查集)

Wireless Network Time Limit: 10000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap co

POJ 2524 Ubiquitous Religions (简单并查集,三种方式)

Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 30791   Accepted: 14928 Description There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in findi

POJ - 2524 Ubiquitous Religions(简单并查集)

Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description 当今世界有很多不同的宗教,很难通晓他们.你有兴趣找出在你的大学里有多少种不同的宗教信仰. 你知道在你的大学里有n个学生(0 < n <= 50000) .你无法询问每个学生的宗教信仰.此外,许多学生不想说出他们的信仰.避免这些问题的一个方法是问

【简单并查集】Farm Irrigation

Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 6   Accepted Submission(s) : 3 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Benny has a spacious fa

poj1611 简单并查集

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 32781   Accepted: 15902 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. T

HDU 1272 简单并查集

小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 24915    Accepted Submission(s): 7641 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双

poj2524(简单并查集)

#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>using namespace std;int n,m;int bin[50001];int findx(int x){    int r=x;    while(r!=bin[r])        r=bin[r];    int j=x,k;    while(j!=r)    {        k=bin