#P3366. Minimum Spanning Tree
Minimum Spanning Tree
Minimum Spanning Tree
Given an undirected graph, your task is to compute the weight of its minimum spanning tree (MST). If the graph is not connected, output orz
instead.
The input starts with two integers n and m representing the number of nodes and edges, respectively. The following m lines each contain three integers u, v, and w, which denote an edge between nodes u and v with weight w.
You are required to use a suitable MST algorithm (such as Kruskal's or Prim's) with the formula for the union-find operation given by \( find(x) = x \) and union by ranking.
inputFormat
The first line contains two integers n
and m
(1 ≤ n ≤ 105, 0 ≤ m ≤ 105).
The next m
lines each contain three integers u
, v
, and w
(1 ≤ u, v ≤ n, 1 ≤ w ≤ 109), denoting an undirected edge between nodes u
and v
with weight w
.
outputFormat
If the graph is connected, output a single integer which is the sum of the weights in the minimum spanning tree. Otherwise, output orz
.
sample
4 5
1 2 1
2 3 2
3 4 3
4 1 4
2 4 2
5