private int[] parent = new int[2501];
private String[] value = new String[2501];
private int find(int a) {
if (parent[a] == a)
return a;
else
return parent[a] = find(parent[a]);
}
private void union(int a, int b) {
a = find(a);
b = find(b);
if (a != b)
parent[b] = a;
}
private int convertNum(int x, int y) {
return 50 * (x - 1) + y;
}
public String[] solution(String[] commands) {
for (int i = 1; i <= 2500; i++) {
parent[i] = i;
value[i] = "";
}
List<String> result = new ArrayList<>();
for (String command : commands) {
StringTokenizer st = new StringTokenizer(command);
String cmd = st.nextToken();
switch (cmd) {
case "UPDATE":
if (st.countTokens() == 2) {
String before = st.nextToken();
String after = st.nextToken();
for (int i = 1; i <= 2500; i++) {
if (before.equals(value[i]))
value[i] = after;
}
} else {
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
String after = st.nextToken();
int num = convertNum(x, y);
value[find(num)] = after;
}
break;
case "MERGE":
int x1 = Integer.parseInt(st.nextToken());
int y1 = Integer.parseInt(st.nextToken());
int x2 = Integer.parseInt(st.nextToken());
int y2 = Integer.parseInt(st.nextToken());
int n1 = convertNum(x1, y1);
int n2 = convertNum(x2, y2);
int root1 = find(n1);
int root2 = find(n2);
if (root1 == root2)
continue;
String rootString = value[root1].isBlank() ? value[root2] : value[root1];
value[root1] = "";
value[root2] = "";
union(root1, root2);
value[root1] = rootString;
break;
case "UNMERGE":
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int num = convertNum(x, y);
int root = find(num);
String rootString1 = value[root];
value[root] = "";
value[num] = rootString1;
List<Integer> deleteList = new ArrayList<>();
for (int i = 1; i <= 2500; i++) {
if (find(i) == root)
deleteList.add(i);
}
for (Integer t : deleteList)
parent[t] = t;
break;
case "PRINT":
int printX = Integer.parseInt(st.nextToken());
int printY = Integer.parseInt(st.nextToken());
int printNum = convertNum(printX, printY);
int printRoot = find(printNum);
if (value[printRoot].isBlank())
result.add("EMPTY");
else
result.add(value[printRoot]);
break;
default:
break;
}
}
return result.toArray(new String[0]);
}
출처:https://school.programmers.co.kr/learn/courses/30/lessons/150366