Loading repovive.com/problems/classics/19
Given an undirected weighted graph with n vertices and m edges, find the weight of the Minimum Spanning Tree (MST) using Kruskal's algorithm.
Kruskal's algorithm sorts edges by weight and greedily adds edges that don't form a cycle, using Union-Find (Disjoint Set Union).
If the graph is not connected, output -1.
4 5 1 2 1 1 3 3 2 3 2 2 4 4 3 4 5
For this input, the required output is 7.
7
3 3 1 2 1 2 3 2 1 3 3
For this input, the required output is 3.
3
4 2 1 2 5 3 4 3
For this input, the required output is -1.
-1