From c6df3730beb4971b63e76f7fafa29454de74a04c Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Fri, 20 Jun 2025 22:48:12 +0900 Subject: [PATCH] =?UTF-8?q?[20250620]=20BOJ=20/=20G4=20/=20=EC=B9=9C?= =?UTF-8?q?=EA=B5=AC=EB=B9=84=20/=20=EA=B9=80=EC=88=98=EC=97=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4 \354\271\234\352\265\254\353\271\204.md" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 "suyeun84/202506/20 BOJ G4 \354\271\234\352\265\254\353\271\204.md" diff --git "a/suyeun84/202506/20 BOJ G4 \354\271\234\352\265\254\353\271\204.md" "b/suyeun84/202506/20 BOJ G4 \354\271\234\352\265\254\353\271\204.md" new file mode 100644 index 00000000..d6f6f4a1 --- /dev/null +++ "b/suyeun84/202506/20 BOJ G4 \354\271\234\352\265\254\353\271\204.md" @@ -0,0 +1,49 @@ +```java +import java.io.*; +import java.util.*; + +public class boj16562 { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringTokenizer st; + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} + static int nextInt() {return Integer.parseInt(st.nextToken());} + + static int N, M, K, answer; + static int[] parent, money; + public static void main(String[] args) throws Exception { + nextLine(); + N = nextInt(); + M = nextInt(); + K = nextInt(); + parent = new int[N+1]; + money = new int[N+1]; + nextLine(); + for (int i = 1; i < N+1; i++) { + parent[i] = i; + money[i] = nextInt(); + } + for (int i = 0; i < M; i++) { + nextLine(); + int a = nextInt(); + int b = nextInt(); + union(a, b); + } + for (int i = 1; i < N+1; i++) { + if (find(i) == i) answer += money[i]; + } + if (K < answer) System.out.println("Oh no"); + else System.out.println(answer); + } + static int find(int x) { + if (x == parent[x]) return x; + return parent[x] = find(parent[x]); + } + + static void union(int x, int y) { + x = find(x); + y = find(y); + if (money[x] < money[y]) parent[y] = x; + else parent[x] = y; + } +} +```