forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path4.java
More file actions
141 lines (114 loc) Β· 3.89 KB
/
4.java
File metadata and controls
141 lines (114 loc) Β· 3.89 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import java.util.*;
class Edge implements Comparable<Edge> {
private int distance;
private int nodeA;
private int nodeB;
public Edge(int distance, int nodeA, int nodeB) {
this.distance = distance;
this.nodeA = nodeA;
this.nodeB = nodeB;
}
public int getDistance() {
return this.distance;
}
public int getNodeA() {
return this.nodeA;
}
public int getNodeB() {
return this.nodeB;
}
// 거리(λΉμ©)κ° μ§§μ κ²μ΄ λμ μ°μ μμλ₯Ό κ°μ§λλ‘ μ€μ
@Override
public int compareTo(Edge other) {
if (this.distance < other.distance) {
return -1;
}
return 1;
}
}
class Position implements Comparable<Position> {
private int x;
private int y;
public Position(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
// XμΆ, YμΆ μμλλ‘ μ λ ¬
@Override
public int compareTo(Position other) {
if (this.x == other.x) {
return Integer.compare(this.y, other.y);
}
return Integer.compare(this.x, other.x);
}
}
public class Main {
// λ
Έλμ κ°μ
public static int n;
public static int[] parent = new int[100001]; // λΆλͺ¨ ν
μ΄λΈ μ΄κΈ°ν
// λͺ¨λ κ°μ μ λ΄μ 리μ€νΈμ, μ΅μ’
λΉμ©μ λ΄μ λ³μ
public static ArrayList<Edge> edges = new ArrayList<>();
public static int result = 0;
// νΉμ μμκ° μν μ§ν©μ μ°ΎκΈ°
public static int findParent(int x) {
// λ£¨νΈ λ
Έλκ° μλλΌλ©΄, λ£¨νΈ λ
Έλλ₯Ό μ°Ύμ λκΉμ§ μ¬κ·μ μΌλ‘ νΈμΆ
if (x == parent[x]) return x;
return parent[x] = findParent(parent[x]);
}
// λ μμκ° μν μ§ν©μ ν©μΉκΈ°
public static void unionParent(int a, int b) {
a = findParent(a);
b = findParent(b);
if (a < b) parent[b] = a;
else parent[a] = b;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
// λΆλͺ¨ ν
μ΄λΈμμμ, λΆλͺ¨λ₯Ό μκΈ° μμ μΌλ‘ μ΄κΈ°ν
for (int i = 1; i <= n; i++) {
parent[i] = i;
}
ArrayList<Position> x = new ArrayList<Position>();
ArrayList<Position> y = new ArrayList<Position>();
ArrayList<Position> z = new ArrayList<Position>();
// λͺ¨λ λ
Έλμ λν μ’ν κ° μ
λ ₯λ°κΈ°
for (int i = 1; i <= n; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
x.add(new Position(a, i));
y.add(new Position(b, i));
z.add(new Position(c, i));
}
Collections.sort(x);
Collections.sort(y);
Collections.sort(z);
// μΈμ ν λ
Έλλ€λ‘λΆν° κ°μ μ 보λ₯Ό μΆμΆνμ¬ μ²λ¦¬
for (int i = 0; i < n - 1; i++) {
edges.add(new Edge(x.get(i + 1).getX() - x.get(i).getX(), x.get(i).getY(), x.get(i + 1).getY()));
edges.add(new Edge(y.get(i + 1).getX() - y.get(i).getX(), y.get(i).getY(), y.get(i + 1).getY()));
edges.add(new Edge(z.get(i + 1).getX() - z.get(i).getX(), z.get(i).getY(), z.get(i + 1).getY()));
}
// κ°μ μ λΉμ©μμΌλ‘ μ λ ¬
Collections.sort(edges);
// κ°μ μ νλμ© νμΈνλ©°
for (int i = 0; i < edges.size(); i++) {
int cost = edges.get(i).getDistance();
int a = edges.get(i).getNodeA();
int b = edges.get(i).getNodeB();
// μ¬μ΄ν΄μ΄ λ°μνμ§ μλ κ²½μ°μλ§ μ§ν©μ ν¬ν¨
if (findParent(a) != findParent(b)) {
unionParent(a, b);
result += cost;
}
}
System.out.println(result);
}
}