Minimum spanning tree after deleting one edge
Time limit3sMemory limit256 MB
For each edge, report the MST weight of the graph with that edge removed, or -1 when it disconnects.
- Level
Medium7 of 10
- Topics
- Minimum spanning tree, Tree, Union-find
- Solved
- No attempts yet
Problem
You are given an undirected weighted graph with nodes and edges. The edges are numbered from to .
Let be the graph obtained by erasing edge from . For each , compute the cost of a minimum spanning tree of .
Input
The input has the following format.
n m
a1 b1 w1
...
am bm wm
The first line contains the number of nodes and the number of edges (, ).
Each of the next lines contains three integers , and (, , ), the -th line describing edge : an edge between node and node with cost .
The graph is guaranteed to be simple. At most one edge connects any pair of nodes, and for every .
Output
Print lines. On line , print the cost of a minimum spanning tree of . If has no spanning tree, print -1 on that line instead.