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.
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
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 NO
otherwise.
Time Complexity:
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.
Time Complexity:
Comments
I am a little puzzled why a Disjoint Set would not be the recommended approach to solving this problem.
DFS is conceptually simpler and more likely to be known by those reading the editorial.