G-圆组

1.4k 词

题目描述

给出n个圆的圆心和半径,相交的圆算在同一组中,如圆1和圆2和圆3相交,则圆1,2,3在同一组中。求总共有几组圆。

输入

多组输入,第一行输入n,表示有n$(0<=n<=1000)$个圆,接下来n行,每行输入 圆心坐标 x,y,半径$r$ (都是int型)

输出

对每组输入输出总共圆的组数

样例输入

1
2
3
4
5
4
2 0 1
0 2 1
-2 0 1
0 -2 1

样例输出

1
4

题解

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
import java.util.Scanner;

public class Main {
static int[] F = new int[1010];

public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
G[] g = new G[n];
for (int i = 0; i < n; i++) {
int x = cin.nextInt();
int y = cin.nextInt();
int r = cin.nextInt();
g[i] = new G(x, y, r);
}
for (int i = 0; i < F.length; i++) {
F[i] = i;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (charge(g[i], g[j]) && i != j) {
union(i, j);
}
}
}
int count = 0;
for (int i = 0; i < n; i++) {
if (F[i] == i) {
count++;
}
}
System.out.println(count);
cin.close();
}

private static void union(int a, int b) {
int fa = find(a);
int fb = find(b);
if (a != b) {
F[fb] = fa;
}
}

private static int find(int a) {
return a == F[a] ? a : (F[a] = find(F[a]));
}

private static boolean charge(G a, G b) {
return dis(a, b) < (a.r + b.r);
}

private static double dis(G a, G b) {
double s = Math.sqrt(1.0 * (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
return s;
}

private static class G {
int x;
int y;
int r;

public G(int x, int y, int r) {
this.x = x;
this.y = y;
this.r = r;
}
}
}