From 4c3f6d6f27821e00a5793f2f6654c7e7f43f00e3 Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Tue, 11 Mar 2025 16:57:52 +0900 Subject: [PATCH] =?UTF-8?q?[20250311]=20BOJ=20/=20G4=20/=20=EC=B5=9C?= =?UTF-8?q?=EC=86=8C=20=EC=8A=A4=ED=8C=A8=EB=8B=9D=20=ED=8A=B8=EB=A6=AC=20?= =?UTF-8?q?/=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 --- ...0\353\213\235 \355\212\270\353\246\254.md" | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 "suyeun84/202503/11 BOJ G4 \354\265\234\354\206\214 \354\212\244\355\214\250\353\213\235 \355\212\270\353\246\254.md" diff --git "a/suyeun84/202503/11 BOJ G4 \354\265\234\354\206\214 \354\212\244\355\214\250\353\213\235 \355\212\270\353\246\254.md" "b/suyeun84/202503/11 BOJ G4 \354\265\234\354\206\214 \354\212\244\355\214\250\353\213\235 \355\212\270\353\246\254.md" new file mode 100644 index 00000000..06c8a63e --- /dev/null +++ "b/suyeun84/202503/11 BOJ G4 \354\265\234\354\206\214 \354\212\244\355\214\250\353\213\235 \355\212\270\353\246\254.md" @@ -0,0 +1,66 @@ +```java +import java.util.*; +import java.io.*; + +public class boj1197 { + 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 ArrayList graph; + static int[] parent; + static int V, E, answer = 0; + public static void main(String[] args) throws Exception { + nextLine(); + V = nextInt(); + E = nextInt(); + int a, b, c; + graph = new ArrayList<>(); + parent = new int[V+1]; + for (int i = 1; i < V+1; i++) parent[i] = i; + + for (int i = 0; i < E; i++) { + nextLine(); + a = nextInt(); + b = nextInt(); + c = nextInt(); + graph.add(new Node(a, b, c)); + } + + graph.sort((o1, o2) -> o1.c-o2.c); + + int edgeCnt = 0; + for (int i = 0; i < E; i++) { + Node curr = graph.get(i); + if (find(curr.s) == find(curr.e)) continue; + answer += curr.c; + union(curr.s, curr.e); + edgeCnt++; + if (edgeCnt == V-1) break; + } + System.out.println(answer); + } + + static void union(int a, int b) { + int pa = find(a); + int pb = find(b); + if (pa != pb) parent[pb] = pa; + } + + static int find(int a) { + if (parent[a] == a) return a; + else return parent[a] = find(parent[a]); + } + + static class Node{ + int s, e, c; + public Node(int s, int e, int c) { + this.s = s; + this.e = e; + this.c = c; + } + } +} + +```