Is it possible to quickly find a common MST between two graphs?
Difference between en5 and en6, changed 2 character(s)
Given two weighted, undirected graphs on $n$ nodes $G_1$ and $G_2$, is it possible to efficiently find a tree on these $n$ nodes that is an MST of both $G_1$ and $G_2$?↵

**UPD:** Building on [user:chromate00,2026-04-23]'s comments, it turns out that we can simply construct a new graph $G$ where the weight of each edge is the sum of that edge's weights in $G_1$ and $G_2$, and find the MST on this graph (in [user:chromate00,2026-04-23]'s notation [here](https://mirror.codeforces.com/blog/entry/144898?#comment-1295911), this means we can actually just always set $C = 1$). If a common MST exists, we can guarantee that this algorithm will always find it.↵

_Proof._ Let $W_1$, $W_2$, and $W$ denote the total weight of MSTs for $G_1$, $G_2$, and $G$ respectively. ↵

- Note that $W_1 + W_2 \leq W$. ↵
- Also note that the weight of any spanning tree in $G_1$ is at least $W_1$, and similarly for $G_2$. Therefore, if we find a spanning tree $T$ with exactly $W = W_1 + W_2$, it must follow that $T$ has exactly weight $W_1$ in $G_1$ and exactly weight $W_2$ in $G_2$, since these are the minimum possible.↵

Here's a problem that uses this idea: [problem:1054G]↵

<spoiler summary="Hint">↵
How can we reframe the problem as finding a simultaneous MST for $m$ graphs?↵
</spoiler>↵

<spoiler summary="Solution">↵
For convenience we will use MST = maximum spanning tree, but all the same principles still apply.↵

For the $i$th community, let $G_i$ be the weighted complete graph in which edge $(u, v)$ has weight 1 if both $u$ and $v$ are in the $i$th community, and weight $0$ otherwise. Then, it is necessary and sufficient that the final tree be a MST ( **maximum** spanning tree) of $G_i$. Therefore, we want to find a simultaneous MST over all $G_i$.↵

From here, we easily arrive at the MST solution given in the [official editorial](https://mirror.codeforces.com/blog/entry/62563) by creating a new graph that sums edge weights across all $G_i$ and finding its MST.↵
</spoiler>↵

(This writeup is also available on my [personal blog](https://danielz.fyi/thoughts/mst)!)

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en6 English shsh 2026-04-23 22:10:08 2 Tiny change: 'tively. \n- Note t' -> 'tively. \n\n- Note t'
en5 English shsh 2026-04-23 09:32:59 192
en4 English shsh 2026-04-23 09:31:03 1913 Tiny change: ' possible.' -> ' possible.\n\nHere's a problem that uses this idea: [problem:1054G]'
en3 English shsh 2025-07-20 09:35:23 8
en2 English shsh 2025-07-20 09:34:43 1 Tiny change: ' both $G_1 and $G_2$' -> ' both $G_1$ and $G_2$'
en1 English shsh 2025-07-20 09:34:01 222 Initial revision (published)