Editorial for DMOPC '18 Contest 6 P3 - Wish Upon a Star
Submitting an official solution before solving the problem yourself is a bannable offence.
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
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)~