Skip to content

Commit 0293426

Browse files
authored
Merge pull request #1682 from AlgorithmWithGod/ksinji
[20251215] BOJ / G5 / 여러분의 다리가 되어드리겠습니다! / 강신지
2 parents eb0d49f + 36d8753 commit 0293426

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static int[] parent;
7+
8+
static int find(int x) {
9+
if (parent[x] == x) return x;
10+
return parent[x] = find(parent[x]);
11+
}
12+
13+
static void union(int a, int b) {
14+
a = find(a);
15+
b = find(b);
16+
if (a != b) parent[b] = a;
17+
}
18+
19+
public static void main(String[] args) throws Exception {
20+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
21+
int n = Integer.parseInt(br.readLine());
22+
23+
parent = new int[n + 1];
24+
for (int i = 1; i <= n; i++) parent[i] = i;
25+
26+
for (int i = 0; i < n - 2; i++) {
27+
StringTokenizer st = new StringTokenizer(br.readLine());
28+
int a = Integer.parseInt(st.nextToken());
29+
int b = Integer.parseInt(st.nextToken());
30+
union(a, b);
31+
}
32+
33+
int root = find(1);
34+
for (int i = 2; i <= n; i++) {
35+
if (find(i) != root) {
36+
System.out.println(1 + " " + i);
37+
return;
38+
}
39+
}
40+
}
41+
}
42+
```

0 commit comments

Comments
 (0)