Блог пользователя avighnakc

Автор avighnakc, история, 12 месяцев назад, По-английски

Since I couldn't find a tutorial on codeforces (or online), I decided to make a blog.

How do we solve MST Edge Check? I think this is one of the newer added problems (+100 from 300 to 400) judging by the number of solves.

If anyone could provide their solution, I'd be very grateful. Thank you!

  • Проголосовать: нравится
  • +12
  • Проголосовать: не нравится

»
12 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

PS: Somewhat relevant, but I also came across this article on MSTs on cp-algorithms: https://cp-algorithms.com/graph/mst_kruskal.html. Here, they mention a proof for Kruskal's algorithm, but it is a bit unclear to me. Specifically, this line:

In case  $T$  doesn't contain the edge  $e$ , then  $T + e$  will contain a cycle  $C$ .

seems at first glance to be contradictory to this one that appears earlier:

If  $e$  generates a cycle, then we don't add it, and so the proposal is still true after this step.

»
12 месяцев назад, скрыть # |
 
Проголосовать: нравится +6 Проголосовать: не нравится
  • build some mst using a method of your choice
  • an edge $$$(u, v, w)$$$ can be an edge in some mst iff the path from $$$u$$$ to $$$v$$$ in the built mst has max edge weight = $$$w$$$
  • »
    »
    12 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится +3 Проголосовать: не нравится

    I did something of this form (though I used randomized labels to compute all of them at the same time with a dfs). However, there is a simpler/alternate solution:

    Alternate Solution
    • »
      »
      »
      12 месяцев назад, скрыть # ^ |
      ← Rev. 2  
      Проголосовать: нравится +1 Проголосовать: не нравится

      So your solution runs Kruskal's algorithm verbatim except it queries connectivity for each weight bucket together, and only then merges. Yeah, this makes a lot of sense too, and is a lot simpler.

      The intuition is that we should imagine the current state of the graph as $$$n$$$ disconnected nodes (compress down components). Then, adding edges of weight $$$w$$$ corresponds to adding edges in this graph, and checking that each one actually merges two nodes (so isn't $$$u \to u$$$) is sufficient since if two $$$w$$$-edges would have ended up merging the same component (like $$$(1,2),(2,3),(3,1)$$$), then we should realise that we can choose the edge we want and cut the remaining (essentially building a spanning tree on these edges).

      The proof for why this works reduces to proving that Kruskal's algorithm itself is correct, since we can then reorder the list of $$$w$$$-edges to place the one we want to pick at the very front.

      Thanks a lot!

  • »
    »
    12 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Great! Just to confirm:

    1. Either the MST edge is $$$\lt$$$ us or $$$=$$$ us. If it's $$$=$$$, we can remove it and add our edge.
    2. If it's $$$ \lt $$$, imagine a supposed "MST" with our edge. We can show that this is not a real MST since we can remove our edge (disconnecting the tree), then add the path from $$$u$$$ to $$$v$$$ in our actual precomputed MST to this tree in the following manner: if $$$(a,b)$$$, an edge on this path, ends up not connecting anything, ignore it. Otherwise, add it. We will end up decreasing the overall weight since the max edge was $$$ \lt $$$ our edge. Also notice that we will only ever add one edge, so there's no "chance" we increase the weight.
»
12 месяцев назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

You can use same solution as 609E

Just check it if mst cost for that edge is the same as mst cost for base graph.

To solve 609E: Create base graph mst. Try to add an edge that does not belong to the mst, it will create a cycle. Now to fix it, we want to erase the maximum weight edge in that cycle so that cost becomes minimum. To find the maximum weight you can use binary lifting.

»
12 месяцев назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

First try to prove that in the MST built with all the edges with weight $$$ \lt w$$$, all the edges with weight $$$w$$$ must join two different components. This thought arises from the fact that for some edge to be included there must not be a cycle with all edges less than its weight which would make it redundant. Easy to prove via exchange arguments like these. So just group edges of same weight together and use DSU.

This same idea can be used to solve the next problem, which involves adding query edges for checking and then rolling them back, then adding the actual tree edges.

»
8 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

for the problem mst edge check, first built a mst and then for rest of the edges which are not added yet into the mst, for such an edge e1=(u,v,c), find the maximum cost edge(e2) in the path from u to v and if that cost is equal to c then the edge(e1) can be added by removing the edge(e2), otherwise not. To find the maximum edge cost, use binary lifting+lca , imagine l=lca(u,v), mx1 is the max cost from u to l and mx2 is the max cost from v to l and mx=max(mx1,mx2)