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!








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:
seems at first glance to be contradictory to this one that appears earlier:
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:
You can bucket edges of the same weight and check all of the ones in the same bucket for whether they can get added, after which you perform the step for Kruskal's normally on each of them and repeat with the next bucket (small to large). This works by extending the observation you made of how an edge can only replace another edge with the same weight, so checking all of the same weight before processing any of them ensures only smaller weights are currently in the MST.
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!
You are spot on! I'm glad I could help :)
Great! Just to confirm:
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.
Thanks a lot, this looks very promising. I've noticed that earlier codeforces educational round problems are always gold mines (a few come to mind, Holes being one of them)
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.
I think JJCUBER's comment talks about the same idea: https://codeforces.me/blog/entry/148479?#comment-1326781
Unfortunately, I don't think I fully understand your solution. Why are we using a DSU?
I understand the first part of your comment: https://codeforces.me/blog/entry/148479?#comment-1326783, you seem to be saying the same thing as point 2 here?
Edit: I think I've understood this now; the DSU is just used by Kruskal's algorithm, which remains unchanged
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)