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 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
Let denote the number of connected components. This can be computed in using DFS.
For of points, we observe that the answer is always yes if or . The first case gives us a forest, and the second case forms a forest with a single extra edge.