Editorial for DMOPC '18 Contest 6 P3 - Wish Upon a Star

Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Kirito

For 20\% of points, we can add each edge, and see if it forms a cycle (namely, it connects two already connected nodes). We count the number of such edges, and output YES if there is at most 1 such edge, and NO otherwise.

Time Complexity: \mathcal O(M^2)

Let C denote the number of connected components. This can be computed in \mathcal O(N + M) using DFS.

For 100\% of points, we observe that the answer is always yes if M = N - C or M = N - C + 1. The first case gives us a forest, and the second case forms a forest with a single extra edge.

Time Complexity: \mathcal O(N + M)


There are no comments at the moment.