From a682979916046367a2302e4efd06d7ead24f8194 Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Wed, 19 Mar 2025 11:03:25 +0900 Subject: [PATCH] =?UTF-8?q?[20250319]=20BOJ=20/=20G4=20/=20=EB=8F=84?= =?UTF-8?q?=EC=8B=9C=20=EB=B6=84=ED=95=A0=20=EA=B3=84=ED=9A=8D=20/=20?= =?UTF-8?q?=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\355\225\240 \352\263\204\355\232\215.md" | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 "suyeun84/202503/19 BOJ G4 \353\217\204\354\213\234 \353\266\204\355\225\240 \352\263\204\355\232\215.md" diff --git "a/suyeun84/202503/19 BOJ G4 \353\217\204\354\213\234 \353\266\204\355\225\240 \352\263\204\355\232\215.md" "b/suyeun84/202503/19 BOJ G4 \353\217\204\354\213\234 \353\266\204\355\225\240 \352\263\204\355\232\215.md" new file mode 100644 index 00000000..16bc2f2f --- /dev/null +++ "b/suyeun84/202503/19 BOJ G4 \353\217\204\354\213\234 \353\266\204\355\225\240 \352\263\204\355\232\215.md" @@ -0,0 +1,68 @@ +```java +import java.io.*; +import java.util.*; + +public class boj1647 { + 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; + static ArrayList roads; + static int[] parent; + + public static void main(String[] args) throws Exception { + nextLine(); + N = nextInt(); + M = nextInt(); + int a, b, c, answer = 0; + roads = new ArrayList<>(); + parent = new int[N+1]; + for (int i = 0; i < N+1; i++) parent[i] = i; + for (int i = 0; i < M; i++) { + nextLine(); + a = nextInt(); + b = nextInt(); + c = nextInt(); + roads.add(new Node(a,b,c)); + } + + roads.sort((o1, o2) -> o1.c-o2.c); + + int cnt = 0; + for (Node road : roads) { + if (cnt == N-2) break; + if (find(road.a) != find(road.b)) { + union(road.a, road.b); + answer += road.c; + cnt++; + } + } + System.out.println(answer); + } + + static void union(int a, int b) { + int A = find(a); + int B = find(b); + if (A==B) return; + if (A