-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ10800.java
More file actions
44 lines (32 loc) · 1015 Bytes
/
Copy pathJ10800.java
File metadata and controls
44 lines (32 loc) · 1015 Bytes
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
package TIL;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class J10800 {
static int n, arr[];
public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(buffer.readLine());
arr = new int[n+1];
for(int i = 0; i<=n; i++){
arr[i] = i;
}
String[] input;
for(int i = 0 ; i < n; i++){
input = buffer.readLine().split(" ");
int n1 = Integer.parseInt(input[0]);
int n2 = Integer.parseInt(input[1]);
union(i+1, n1);
// 아;
}
}
public static int find(int a){
if(a == arr[a]) return a;
return arr[a] = find(arr[a]);
}
public static void union(int a, int b){
int max = Math.max(find(a), find(b));
int min = Math.min(find(a), find(b));
arr[max] = min;
}
}