#K77287. Minimum Spanning Tree
Minimum Spanning Tree
Minimum Spanning Tree
You are given an undirected weighted graph \(G=(V,E)\) with \(n\) vertices and \(m\) edges. Your task is to compute the total weight of the Minimum Spanning Tree (MST) formed by a subset of the edges. The MST is defined as a spanning tree whose sum of edge weights is minimum among all spanning trees. If the graph is disconnected, calculate the sum of the weights of the minimum spanning forest.
The input consists of two integers \(n\) and \(m\) on the first line. The next \(m\) lines each contain three integers \(u\), \(v\), and \(w\), representing an edge between vertices \(u\) and \(v\) having weight \(w\). Note that the vertices are numbered from 1 to \(n\).
Your solution should read input from stdin
and output the total MST weight to stdout
.
inputFormat
The first line of input contains two space-separated integers \(n\) (number of vertices) and \(m\) (number of edges).
Each of the following \(m\) lines contains three space-separated integers \(u\), \(v\), and \(w\), meaning there is an edge between vertex \(u\) and vertex \(v\) with weight \(w\).
outputFormat
Output a single integer representing the total weight of the Minimum Spanning Tree (or forest, if the graph is disconnected).
## sample4 5
1 2 1
1 3 4
2 3 2
2 4 7
3 4 3
6
</p>