Under construction

Still being built. Things may change

Broken Minimum Spanning Tree

Time limit1sMemory limit2048 MB

Summary
Given a spanning tree of a connected graph, find the fewest edge swaps that turn it into a minimum spanning tree and list those swaps.
Level

Medium7 of 10

Topics
Minimum spanning tree, Graph
Solved
No attempts yet

Problem

Ethan was tasked with finding a minimum spanning tree of a weighted, connected, undirected graph. However, he misunderstood the task and found a spanning tree that may not be minimal. To make his spanning tree a minimum spanning tree, you perform a sequence of edge swaps. An edge swap removes one edge from the spanning tree and adds an edge from the graph that is not currently in the spanning tree. After each edge swap, the tree must still be a spanning tree. What is the minimum number of edge swaps you must perform to fix Ethan's minimum spanning tree?

Input

The first line contains two integers nn (2≤n≤2 0002 \le n \le 2\,000) and mm (n−1≤m≤3 000n-1 \le m \le 3\,000), where nn is the number of nodes and mm is the number of edges. The nodes are numbered from 11 to nn.

Each of the next mm lines contains three integers uu, vv (1≤u,v≤n1 \le u,v \le n, u≠vu \ne v), and ww (1≤w≤1091 \le w \le 10^9), meaning an edge connects nodes uu and vv with weight ww. The edges are numbered from 11 to mm.

The graph is connected. The first n−1n-1 edges are Ethan's initial spanning tree. The graph may not be simple, so there can be multiple edges between the same pair of nodes.

Output

Output a single integer kk, the minimum number of edge swaps needed to make the spanning tree a minimum spanning tree. Then output kk lines, each with two integers aa and bb, where aa is the number of the edge to remove and bb is the number of the edge to add. If several sets of kk swaps work, any one is accepted.

Examples1

  1. Example 1

    Input
    4 4
    1 2 10
    2 3 3
    3 4 1
    1 4 4
    
    Expected output
    1
    1 4