Given a weighted, undirected and connected graph. The task is to find the sum of weights of the edges of the Minimum Spanning Tree.

**Input:**

The first line of input contains an integer **T** denoting the number of testcases. Then T test cases follow. The first line of each testcase contains two integers N (starting from 1), E denoting the number of nodes and number of edges. Then in the next line are 3*E space separated values a b w where a, b denotes an **edge** from **a** to **b** and **w** is the weight of the edge.

**Output:**

For each test case in a new line print the sum of weights of the edges of the Minimum Spanning Tree formed of the graph.

**User task:**

Since this is a functional problem you don't have to worry about input, you just have to complete the function **spanningTree()** which takes a graph **g** as its argument and returns an integer denoting the sum of weights of the edges of the Minimum Spanning Tree.

**Constraints:**

1 <= T <= 10

1 <= N<= 100

N-1 <= E<= 1000

1 <= w <= 1000

**Example:
Input**:

2

3 3

1 2 5 2 3 3 1 3 1

2 1

1 2 5

**Output**:

4

5

**Example:
Testcase 1: ** Sum of weights of edges in the minimum spanning tree is 4.

Author: Shubham Joshi 1

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

sabka_din_aayega | 317 |

maggiiiii | 248 |

thanuvinu94 | 211 |

Savage_19 | 206 |

Anugrah_kumar | 172 |

Bishnu Dev Panda | 803 |

sabka_din_aayega | 628 |

Anugrah_kumar | 562 |

okayboss | 561 |

maggiiiii | 531 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4925 |

Quandray | 4547 |

Login to report an issue on this page.