๊ฐ์ ์ค์ฌ์ผ๋ก ์ต์ ์ ์ฅ ํธ๋ฆฌ๋ฅผ ๊ตฌํ๋ ์๊ณ ๋ฆฌ์ฆ์ผ๋ก, ๊ฐ์ ์ด ์ ์ ๋ ์ ๋ฆฌํฉ๋๋ค.
๐ก ํฌ๋ฃจ์ค์นผ ์๊ณ ๋ฆฌ์ฆ ๊ตฌํ ๋ฐฉ๋ฒ
1. ๊ฐ์ ๋ค์ ๊ฐ์ค์น์ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํฉ๋๋ค.
2. ๊ฐ์ค์น๊ฐ ๊ฐ์ฅ ์์ ๊ฐ์ ์ ์ ํํ์ฌ ๊ทธ ๊ฐ์ ์ด ์ง๊ธ๊น์ง ๋ง๋ค์ด์ง MST์ ์ฌ์ดํด์ ํ์ฑํ๋ค๋ฉด ์ ์ธํ๊ณ , ์๋๋ฉด MST์ ์ถ๊ฐํฉ๋๋ค.
3. ๋ชจ๋ ๊ฐ์ ์ ๋ํด ๋ฐ๋ณตํฉ๋๋ค.
๋ฐฑ์ค 1197๋ฒ ์ต์ ์คํจ๋ ํธ๋ฆฌ
import java.io.*;
import java.util.*;
public class Main {
static int find(int x, int[] parent) {
if (parent[x] == x) return x;
int nx = find(parent[x], parent);
parent[x] = nx;
return nx;
}
static boolean union(int x, int y, int[] parent) {
x = find(x, parent);
y = find(y, parent);
if (x == y) return false;
parent[y] = x;
return true;
}
static int kruskal(int V, int E, Edge[] edges, int[] parents) {
int answer = 0, cnt = 0;
for (int i = 1; i <= V; i++)
parents[i] = i;
for (Edge edge: edges) {
if (!union(edge.from, edge.to, parents)) continue;
answer += edge.weight;
cnt++;
if (cnt == V - 1) return answer;
}
return answer;
}
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());
Edge[] edges = new Edge[E];
int[] parents = new int[V + 1];
int A, B, C;
for (int i = 0; i < E; i++) {
st = new StringTokenizer(br.readLine());
A = Integer.parseInt(st.nextToken());
B = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
edges[i] = new Edge(A, B, C);
}
Arrays.sort(edges);
System.out.println(kruskal(V, E, edges, parents));
br.close();
}
static class Edge implements Comparable<Edge> {
int from;
int to;
int weight;
Edge(int from, int to, int weight) {
this.from = from;
this.to = to;
this.weight = weight;
}
@Override
public int compareTo(Edge o) {
return this.weight - o.weight;
}
}
}