Algorithm
Compute MST T. For each non-tree edge e, add e — creates cycle. Remove max non-e edge from cycle. Track best swap.
Advertisement
Complexity
Preprocess: LCA + max edge on path queries via HLD or sparse table. Each swap O(log V). Total O(V log V + E log V).
Advertisement
Kirchhoff's alternative
For counting all MSTs (matrix-tree theorem). Second-best via generating function on determinant.
Applications
Backup network design. Sensitivity analysis: how sensitive is MST to single edge weight changes.