Objective
A minimum spanning tree connects all the nodes at the lowest possible total cost and without cycles. Kruskal’s algorithm sorts the edges by weight and adds each one if it does not close a cycle, which is checked with union-find.
Instructions
- The first line has
n m; thenmlinesu v w(undirected edge with weight). - Show
Minimum cost: xwith the sum of weights of the tree, orGraph not connectedif the graph is not connected.
4 4
0 1 1
1 2 2
0 2 3
2 3 4
Minimum cost: 7
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise