Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions Seol-JY/202506/25 BOJ G4 최소 스패닝 트리.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
```java
import java.util.*;
import java.io.*;

public class Main {
static int[] parent;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int V = Integer.parseInt(st.nextToken());
int E = Integer.parseInt(st.nextToken());

List<Edge> edgeList = new ArrayList<>();
parent = new int[V + 1];

for (int i = 1; i <= V; i++) {
parent[i] = i;
}

for (int i = 0; i < E; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int weight = Integer.parseInt(st.nextToken());
edgeList.add(new Edge(a, b, weight));
}

edgeList.sort((e1, e2) -> Integer.compare(e1.weight, e2.weight));

int total = 0;
int count = 0;

for (Edge edge : edgeList) {
if (find(edge.from) != find(edge.to)) {
union(edge.from, edge.to);
total += edge.weight;
count++;
if (count == V - 1) break;
}
}

System.out.println(total);
}

static int find(int x) {
if (parent[x] == x) return x;
return parent[x] = find(parent[x]);
}

static void union(int a, int b) {
int rootA = find(a);
int rootB = find(b);
if (rootA != rootB) parent[rootB] = rootA;
}

static class Edge {
int from, to, weight;

Edge(int from, int to, int weight) {
this.from = from;
this.to = to;
this.weight = weight;
}
}
}

```