Editorial for An Animal Contest 7 P2 - Squirrel Structures
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:
Hint 1
What happens with node and its children?Hint 2
will be at least the number of children node has . Because of constraints and , neither node nor its children can be a child of another node.Solution
It turns out that the minimal will always be the number of children node has . To construct a satisfactory set of trees, we can set node and its children as the root of each of the trees. In order to satisfy constraint , for each node that is not already part of a tree we either add it to the tree with node as root, or the child of node that is an ancestor of node as root — meaning we have two choices. This is enough to satisfy constraint : the remaining nodes can be added to either of those two choices depending on the parity of its depth.In essence, we just need to add each node to the parent of its parent.
Time Complexity:
Comments