Minimum Spanning Tree Total Weight

medium · graph, mst, union-find

Minimum Spanning Tree Total Weight

Given n nodes (0-indexed) and a list of undirected weighted edges [u, v, w], return the total weight of a minimum spanning tree (MST). The graph is connected.

Function signature

func MSTTotalWeight(n int, edges [][]int) int64

Example

n = 4
edges = [[0,1,1],[1,2,2],[0,2,2],[2,3,1],[1,3,3]]
output = 4

Constraints

  • 1 <= n <= 200_000
  • n - 1 <= len(edges) <= 200_000
  • 0 <= u, v < n
  • -1_000_000_000 <= w <= 1_000_000_000

Notes

  • Use Kruskal (sort edges + union-find) or Prim (heap).
Run tests to see results
No issues detected