609E — MST for Each Edge: Using a Kruskal Property Without LCA

Revision en2, by Tabriz-Talayi, 2026-09-06 13:42:35

Context:
I recently learnt MST and reached this problem (609E - Minimum spanning tree for each edge). I was stuck on it for like 2 days, and I only managed to come up with an $$$O(m\logm + mn)$$$ solution, which will obviously TLE. I decided to read some of my teachers'/friends'/top coders' codes to get some idea, but they all used some kind of heavy algorithm I haven't learnt yet (mostly LCA). I was ready to give up on the problem; I decided to hand-run the current algorithm I had one more time to look for improvements before giving up, and fortunately during hand-running I noticed a key property in the Kruskal algorithm which led to me coming up with an extremely clean sol.
It solves the problem in $$$O(m\logm + (n + m\logn)\logn)$$$ and uses the core Kruskal Algorithm plus a vector-based StoL DSU.
IDK if this is a well-known property or not, but after checking some submissions, I'm sure it's not a well-known approach for this problem.


The Property

Full Solution
Implementation

I'm new to CP and hand-wrote everything myself (I used AI just for notation and capitalization and stuff), so if anyone has a better way of explaining than I do, or if some of what I said is wrong or needs editing, please let me know and I will edit the blog to bring it up to standard.

Tags mst, dsu, kruskal

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en4 English Tabriz-Talayi 2026-09-06 13:53:02 112 Tiny change: 'us\n<br>\nOtherwis' -> 'us\n<br>\n\nOtherwis'
en3 English Tabriz-Talayi 2026-09-06 13:46:21 5
en2 English Tabriz-Talayi 2026-09-06 13:42:35 7
en1 English Tabriz-Talayi 2026-09-06 13:39:35 6008 Initial revision (published)