From 4561e175145f1bec3599b12a3a108e6575ad7873 Mon Sep 17 00:00:00 2001 From: Donew <47556347+03do-new30@users.noreply.github.com> Date: Mon, 21 Jul 2025 17:20:23 +0900 Subject: [PATCH] =?UTF-8?q?[20250721]=20BOJ=20/=20G5=20/=20=EC=97=AC?= =?UTF-8?q?=EB=9F=AC=EB=B6=84=EC=9D=98=20=EB=8B=A4=EB=A6=AC=EA=B0=80=20?= =?UTF-8?q?=EB=90=98=EC=96=B4=EB=93=9C=EB=A6=AC=EA=B2=A0=EC=8A=B5=EB=8B=88?= =?UTF-8?q?=EB=8B=A4!=20/=20=EC=8B=A0=EB=8F=99=EC=9C=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\354\212\265\353\213\210\353\213\244!.md" | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 "03do-new30/202507/21 BOJ G5 \354\227\254\353\237\254\353\266\204\354\235\230 \353\213\244\353\246\254\352\260\200 \353\220\230\354\226\264\353\223\234\353\246\254\352\262\240\354\212\265\353\213\210\353\213\244!.md" diff --git "a/03do-new30/202507/21 BOJ G5 \354\227\254\353\237\254\353\266\204\354\235\230 \353\213\244\353\246\254\352\260\200 \353\220\230\354\226\264\353\223\234\353\246\254\352\262\240\354\212\265\353\213\210\353\213\244!.md" "b/03do-new30/202507/21 BOJ G5 \354\227\254\353\237\254\353\266\204\354\235\230 \353\213\244\353\246\254\352\260\200 \353\220\230\354\226\264\353\223\234\353\246\254\352\262\240\354\212\265\353\213\210\353\213\244!.md" new file mode 100644 index 00000000..f1403944 --- /dev/null +++ "b/03do-new30/202507/21 BOJ G5 \354\227\254\353\237\254\353\266\204\354\235\230 \353\213\244\353\246\254\352\260\200 \353\220\230\354\226\264\353\223\234\353\246\254\352\262\240\354\212\265\353\213\210\353\213\244!.md" @@ -0,0 +1,65 @@ +```java +import java.util.*; +import java.io.*; + +public class Main { + static int N; + static List[] graph; + static int[] parents; + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + N = Integer.parseInt(br.readLine()); + graph = new List[N+1]; + for (int i = 1; i <= N; i++) { + graph[i] = new ArrayList<>(); + } + makeParents(); + for (int i = 0; i < N-2; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + graph[a].add(b); + graph[b].add(a); + union(a, b); + } + boolean found = false; + for (int i = 1; i < N; i++) { + if (found) break; + for (int j = i + 1; j <= N; j++) { + if (find(i) == find(j)) { + continue; + } + System.out.println(i + " " + j); + found = true; + break; + } + } + br.close(); + + } + + static void makeParents() { + parents = new int[N+1]; + for (int i = 1; i <= N; i++) { + parents[i] = i; + } + } + + static int find(int a) { + if (parents[a] == a) {return a;} + return parents[a] = find(parents[a]); + } + + static boolean union(int a, int b) { + int rootA = find(a); + int rootB = find(b); + if (rootA == rootB) {return false;} + if (rootA < rootB) { + parents[rootB] = rootA; + } else { + parents[rootA] = rootB; + } + return true; + } +} +```