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