I've noticed that in the tutorial the author mentioned that there exist an linear time solution, but I don't know what the solution is ...
| # | User | Rating |
|---|---|---|
| 1 | Benq | 3792 |
| 2 | VivaciousAubergine | 3647 |
| 3 | Kevin114514 | 3603 |
| 4 | jiangly | 3583 |
| 5 | strapple | 3515 |
| 6 | tourist | 3470 |
| 7 | dXqwq | 3436 |
| 8 | Radewoosh | 3415 |
| 9 | Otomachi_Una | 3413 |
| 10 | Um_nik | 3376 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 158 |
| 2 | adamant | 152 |
| 3 | Um_nik | 146 |
| 4 | Dominater069 | 144 |
| 5 | errorgorn | 141 |
| 6 | cry | 139 |
| 7 | Proof_by_QED | 137 |
| 8 | YuukiS | 135 |
| 9 | chromate00 | 134 |
| 9 | TheScrasse | 134 |
I've noticed that in the tutorial the author mentioned that there exist an linear time solution, but I don't know what the solution is ...
| Name |
|---|



Find all the biconnected components, let there be $$$C$$$ such components.
Let $$$G$$$ be a graph with $$$C$$$ nodes. For some color $$$c$$$ let the indices of edges with such color be $$$c_1, c_2, \ldots, c_x$$$, let's add edge $$$c_i-c_{i-1}$$$ for every $$$x \geq i \gt 1$$$ to $$$G$$$. If we do this for all colors we will have some connected components in $$$G$$$. It can be proven that for every connected component in $$$G$$$: we can have all the colors (in this component) in an optimal tree iff this component has a cycle or contains a vertex which corresponds to a bridge BCC in the original graph.
All of this can be done in linear time.