-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSB_150366.java
More file actions
125 lines (108 loc) Β· 3.94 KB
/
SB_150366.java
File metadata and controls
125 lines (108 loc) Β· 3.94 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
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
class SB_150366 {
static String[] board = new String[2500];
static int[] parents = new int[2500];
static List<String> ans = new ArrayList<>();
static String empty = "EMPTY";
static int N = 2500;
private static void print(String[] cmd) {
int r = Integer.parseInt(cmd[1])-1;
int c = Integer.parseInt(cmd[2])-1;
int idx = r*50+c;
int pIdx = find(idx);
if (board[pIdx]==null) ans.add(empty);
else ans.add(board[pIdx]);
}
private static void unmerge(String[] cmd) {
int r = Integer.parseInt(cmd[1])-1;
int c = Integer.parseInt(cmd[2])-1;
int idx = r*50+c; // μ νλ r,c μ
μΈλ±μ€
int p = find(idx); // ν΄λΉ μ
μ λΆλͺ¨ μΈλ±μ€ (λ³ν© λν)
String val = board[p]; // λΆλͺ¨ μΈλ±μ€μ κ° (λ³ν© λν κ°)
// λ³ν© ν΄μ λ μ
μΆμ 리μ€νΈ
List<Integer> candi = new ArrayList<>();
// λ³ν©ν΄μ ν μ
νλ³΄λ‘ λ΄κΈ°
for (int i = 0; i < N; i++) {
if (find(i) == p) {
candi.add(i);
}
}
// λ³ν©ν΄μ ν μ
κ° λ³΅μ
for (Integer cell : candi){
parents[cell] = cell;
board[cell] = null;
}
// νκ² λ³ν©ν΄μ μ
μ μλ κ° μ μ§
board[idx] = val;
}
private static int find(int x) {
if (x!=parents[x]) parents[x] = find(parents[x]);
return parents[x];
}
private static void union(int a, int b, String val) {
if (a != b) {
parents[b] = a; // bμ λΆλͺ¨λ₯Ό aλ‘ μ€μ
board[a] = val; // λ³ν© λνμλ§ κ° μ€μ
// bμ μ°κ²°λ λͺ¨λ μ
μ μλ‘μ΄ λΆλͺ¨ aμ€μ
for (int i = 0; i < N; i++) {
if (find(i)==b) parents[i] = a;
}
}
}
private static void merge(String[] cmd) {
int r1 = Integer.parseInt(cmd[1])-1;
int c1 = Integer.parseInt(cmd[2])-1;
int r2 = Integer.parseInt(cmd[3])-1;
int c2 = Integer.parseInt(cmd[4])-1;
// κ° μ
μ λΆλͺ¨ μΈλ±μ€
int p1 = find(50*r1+c1);
int p2 = find(50*r2+c2);
// λν μ
κ° κ²°μ
if (p1 != p2) {
String val = board[p1] != null ? board[p1] : board[p2];
union(p1, p2, val);
}
}
private static void updateOne(String[] cmd) {
int r = Integer.parseInt(cmd[1]) - 1;
int c = Integer.parseInt(cmd[2]) - 1;
// μ νν μ
μ λΆλͺ¨ κ° μμ
int idx = 50*r+c; // μ νλ κ°μ μΈλ±μ€
int p = find(idx); // ν΄λΉ μΈλ±μ€μ λΆλͺ¨(λ³ν© λν)
board[p] = cmd[3]; // λ³ν© λνκ° μ
λ°μ΄νΈ
}
private static void updateAll(String[] cmd) {
String target = cmd[1];
String val = cmd[2];
for (int i = 0; i < N; i++) { // λ°°μ΄ μννλ©΄μ κ° λ³κ²½
if (Objects.equals(board[i], target)) board[i] = val; // nullκ° μ²λ¦¬(Objects.equals)
}
}
public static String[] solution(String[] commands) {
// λ³ν©ν
μ΄λΈ μ΄κΈ°ν
for (int i = 0; i < N; i++) {
parents[i] = i;
}
for (String cmds : commands) {
String[] cmd = cmds.split(" ");
switch (cmd[0]) {
case "UPDATE":
if (cmd.length == 4) updateOne(cmd);
else updateAll(cmd);
break;
case "MERGE":
merge(cmd);
break;
case "UNMERGE":
unmerge(cmd);
break;
case "PRINT":
print(cmd);
break;
}
}
return ans.toArray(new String[0]);
}
}