UVa 10462 - Is There A Second Way Left?

10462 - Is There A Second Way Left ?

 * http://acm.uva.es/p/v104/10462.html

Summary
given a weighted undirected graph, we have to find the second best MST.

Explanation
Let Us take an example, let there be 5 edges in a graph G labeled 1..5.
 * 1) first run a dfs or bfs to see if the graph is connected, if no then output no way
 * 2) now find the MST using kruskal's algorithm ( prim will also do )and mark all the edges which is part of the MST.
 * 3) and now ignore the edges which are part of the MST, one at a time and try to build a new MST with the available edges[becareful that the MST must be connected].and find the weight of MST . among  all the connected MST's find the minimum weight which is the answer . if there is no connected MST , then output no second way ....

let the MST considering all the edges, consists of edges 1 , 3 , 5. now ignore edge 1 only and consider edge 2, 3, 4 , 5 to build an MST if it is possible to build an mst calculate its weight now ignore edge 3 only do the same now ignore edge 5 only and do the same among all the MST's the minium weight one is the second best mst.

Gotchas

 * MST must be connected

Implementations
Notes/Hints on actual implementation here.

Optimizations
Optimizations here.

Input
Input Here

Output
Output Here