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\log m + 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\log m + (n + m\log n)\log n)$$$ 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.
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.









Auto comment: topic has been updated by Tabriz-Talayi (previous revision, new revision, compare).
Auto comment: topic has been updated by Tabriz-Talayi (previous revision, new revision, compare).
Auto comment: topic has been updated by Tabriz-Talayi (previous revision, new revision, compare).