14327 - 2024_IDS_Spring_Lab10_Minimum_Spanning_Tree   

Description

One day Mr. K gives you a recipe:

```
Section I   Ingredients
   - A weighted connected undirected graph G with n vertices and m edges
   - Another graph T using same vertices with G but having no edge

Section II  Procedure
   1. Pick an edge e with smallest weight among unprocessed edges in graph G.
      (If there are more than one such edges, pick any.)
   2. If edge e forms an cycle along with the edges in graph T, throw edge e away.
      Otherwise, add edge e into graph T.
   3. Repeat step 1 and 2 until all edges in graph G are processed.
   4. Finally, you construct a cool graph T!
```

You are so curious what this recipe is about.  Luckily, you always bring a weighted connected undirected graph with you so you can start following the recipe right away.  Please follow the instruction in the recipe to construct a cool graph (the T mentioned in the recipe) and output its total edge weight.

Input

The first line contains two integers n and m, being the number of vertex and the number of edges in your weighted undirected graph.

The following m lines describes the edges.  Each line contains three integers u, v, w, denoting an undirected edge uv with weight w.

**Constraints**

- 1 <= n <= 2 * 105
- 1 <= m <= 106
- 1 <= w <= 109 for each edge

Output

Please print the answer in one line.

Sample Input  Download

Sample Output  Download

Tags




Discuss