Can anybody explain me how to find the second minimum spanning tree in the fastest way, I heard about disjoint sets I studied them but I still can't find the way to do it...

Before contest

Codeforces Round #602 (Div. 1, based on Technocup 2020 Elimination Round 3)

36:39:23

Register now »

Codeforces Round #602 (Div. 1, based on Technocup 2020 Elimination Round 3)

36:39:23

Register now »

*has extra registration

Before contest

Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3)

36:39:23

Register now »

Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3)

36:39:23

Register now »

*has extra registration

# | User | Rating |
---|---|---|

1 | tourist | 3532 |

2 | Radewoosh | 3434 |

3 | wxhtxdy | 3425 |

4 | Benq | 3368 |

5 | ecnerwala | 3301 |

6 | mnbvmar | 3280 |

7 | LHiC | 3276 |

8 | maroonrk | 3199 |

9 | yutaka1999 | 3190 |

10 | TLE | 3145 |

# | User | Contrib. |
---|---|---|

1 | Errichto | 192 |

2 | Radewoosh | 178 |

3 | tourist | 173 |

4 | antontrygubO_o | 167 |

4 | Vovuh | 167 |

4 | PikMike | 167 |

7 | rng_58 | 160 |

8 | majk | 157 |

9 | Um_nik | 152 |

9 | farmersrice | 152 |

Can anybody explain me how to find the second minimum spanning tree in the fastest way, I heard about disjoint sets I studied them but I still can't find the way to do it...

↑

↓

Codeforces (c) Copyright 2010-2019 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Nov/22/2019 22:25:38 (h1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

discard minimum cost & run MST code again.

I guess follow solution can be correct.

build MST. Then build a tree from edges of MST. Now if you try to add any other edge into a tree it makes a cycle in the new graph. find the edge with the maximal cost on the cycle and remove it from graph. Now graph is a tree again. Try all edges and choose the minimal possible tree (make shure it is not another MST). you can find maximal edge on the cycle in LogN using LCA(LeastCommonAncestor) and some other data structures.

O(M logN) for Kruskal and O(M logN) again for M queries with LCA. It will work.

thank you, I also came up with the idea of disjoint-sets to check if there is a cycle. Anw thanks everybody for helping.

First off, build the minimum spanning tree, then, for each edge of the minimum spanning tree, build a new one discarding such edge. The minimum one will be the second minimum spanning tree. Using Kruskal algorithm and a O(n * log(n)) sorting algorithm for the edges, this strategy lead to a O(N^2 * log(N)) overall complexity.

You can make use of the LCA algorithm and the observation that the second mst will differ from the first by only one edge to make the algorithm described by aajjbb run in O( ElogV ) time

Could you please provide some code for this O(ElogV) algorithm ?

You can read here

We can achieve this in O(E * log(N)) time using heavy-light decomposition. E = Number of Edges You can find more about heavy light decomposition here : Heavy Light Decomposition

Basically for every edge which you consider to add into the MST(Minimum Spanning Tree) to make it second minimum spanning tree, let us say it connects two vertices A and B.

Now after adding this new edge we need to remove one edge which is the smallest among the edges that lie in the path between A and B in the original MST. This can be achieved using Heavy Light Decomposition on the MST in Log(N) time for one edge. So it takes O(E * Log(N)) total time.

Source: https://www.quora.com/How-do-I-get-a-second-minimum-spanning-tree (Answer by adurysk)

Your general idea is correct, though you need to remove the largest edge on the path from A to B, not the smallest one. And also, you can achieve that without heavy-light decomposition. Two LCA-like data structures will solve it (one for ancestors, one for maximum edges).