#C10094. Minimum Spanning Tree using Kruskal's Algorithm

    ID: 39261 Type: Default 1000ms 256MiB

Minimum Spanning Tree using Kruskal's Algorithm

Minimum Spanning Tree using Kruskal's Algorithm

You are given an undirected connected graph with V vertices and E edges. Each edge is given as a triple of integers (U, V, L) which represents an edge between vertex U and V with weight L. Your task is to compute the total weight of the Minimum Spanning Tree (MST) of the graph using Kruskal's algorithm.

Note: It is guaranteed that the input graph is connected.

The MST is defined as a subset of the edges that connects all the vertices together without any cycles and with the minimum possible total edge weight.

Hint: Use the union-find (disjoint set union) data structure to efficiently determine whether adding an edge would create a cycle.

inputFormat

The first line contains an integer T representing the number of test cases. Each test case begins with a line containing two integers V (number of vertices) and E (number of edges). The next E lines each contain three integers U, V and L describing an edge between vertices U and V with weight L.

outputFormat

For each test case, output a single line containing the total weight of the MST.

## sample
2
4 5
1 2 10
1 3 6
1 4 5
2 3 4
3 4 7
3 3
1 2 3
2 3 5
1 3 4
15

7

</p>