pku 2236
2009年7月13日 星期一
題目鏈接:PKU 2236 Wireless Network
分類:并查集的應用
Code:
1
#include<stdio.h>
2
#include<string.h>
3
#include<math.h>
4
#define max 1005
5
int i,j,parent[max],t,n,d,a,b,f[max];
6
char s[5];
7
double dis[max][max];
8
struct zuobiao
9

{
10
double x,y;
11
}pos[max];
12
void init(int n)
13

{
14
for(j=1;j<=n;j++)parent[j]=-1;
15
}
16
int find(int x)
17

{
18
if(parent[x]<0)return x;
19
else return parent[x]=find(parent[x]);
20
}
21
void Union(char k)
22

{
23
if(k=='O')
24
{
25
f[a]=1;
26
for(j=1;j<=n;j++)
27
{
28
if(a!=j&&f[j]&&dis[a][j]<=d)
29
{
30
int r1=find(a),r2=find(j);
31
if(r1!=r2) //以root2為根
32
{
33
parent[r2]+=parent[r1];
34
parent[r1]=r2;
35
}
36
}
37
}
38
}
39
else
40
{
41
int r1=find(a),r2=find(b);
42
if(r1==r2)printf("SUCCESS\n");
43
else printf("FAIL\n");
44
}
45
}
46
int main()
47

{
48
scanf("%d%d",&n,&d);
49
init(n);
50
memset(f,0,sizeof(f));
51
memset(dis,0,sizeof(dis));
52
for(i=1;i<=n;i++)scanf("%lf%lf",&pos[i].x,&pos[i].y);
53
for(i=1;i<n;i++)
54
for(j=i+1;j<=n;j++)
55
{
56
dis[i][j]=sqrt((pos[i].x-pos[j].x)*(pos[i].x-pos[j].x)+(pos[i].y-pos[j].y)*(pos[i].y-pos[j].y));
57
dis[j][i]=dis[i][j];
58
}
59
while(scanf("%s",s)!=EOF)
60
{
61
if(strcmp(s,"S")==0)scanf("%d%d",&a,&b);
62
else scanf("%d",&a);
63
Union(s[0]);
64
}
65
return 0;
66
}
67
題目鏈接:PKU 2236 Wireless Network
分類:并查集的應用
Code:
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

45

46

47



48

49

50

51

52

53

54

55



56

57

58

59

60



61

62

63

64

65

66

67
