[POJ] [2236] Wireless Network

Problem Description

Loading...

Problem Analysis

I’ve been slacking off for several days~
Let’s catch up on some practice.

The idea for this problem is to combine it with Union-Find operations.

When encountering ‘O’
search all points
to find those that satisfy

  1. The computer has been repaired
  2. The distance to this computer is no greater than D
    When satisfied
    a union operation can be performed

For this, we need to record and calculate the distances between points.

When encountering S
check whether the two points are in the same set

PS:

  1. Be careful to absorb newlines (getchar();)
  2. Don’t misspell the words (don’t ask me how I know…)
cpp
 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
68
#include<stdio.h>
#include<math.h>
#include<string.h>
int par[1020];
int rank[1020];
int find(int m) {
	if(par[m]==m)
		return m;
	else {
		return par[m]=find(par[m]);
	}
}
void unite(int x,int y) {
	x=find(x);
	y=find(y);
	if(x==y)
		return;
	else {
		if(rank[x]<rank[y]) {
			par[x]=y;
		} else {
			par[y]=x;
			if(rank[x]==rank[y]) {
				rank[x]++;
			}
		}
	}
}

int main() {
	int N,D;
	scanf("%d %d",&N,&D);
	int xy[1020][2];
	int flag[1020];
	memset(flag,0,sizeof(flag));
	for(int i=1; i<=N; i++) {
		par[i]=i;
		rank[i]=0;
	}
	for(int i=1; i<=N; i++) {
		scanf("%d %d",&xy[i][0],&xy[i][1]);
	}
	char c;
	getchar();
	while(scanf("%c",&c)!=EOF) {
		if(c=='O') {
			int t;
			scanf("%d",&t);
			flag[t]=1;
			for(int i=1; i<=N; i++) {
				if(flag[i]==1&&i!=t) {
					if((xy[i][0]-xy[t][0])*(xy[i][0]-xy[t][0])+(xy[i][1]-xy[t][1])*(xy[i][1]-xy[t][1])<=D*D) {
						unite(i,t);
					}
				}
			}
		} else if(c=='S') {
			int p1,p2;
			scanf("%d %d",&p1,&p2);
			if(find(p1)==find(p2)) {
				printf("SUCCESS\n");
			} else
				printf("FAIL\n");
		}
		getchar();
	}
	return 0;
}

Problem URL: POJ 2236 Wireless Network

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...