E. Tree Colorings
Problem Statement
Consider a rooted undirected tree. Each vertex can be colored blue, green, or yellow. A coloring is called beautiful if it meets these conditions:
- The root of the tree is green
- If you consider all blue and green vertices, they are reachable from each other without passing through any yellow vertices
- If you consider all yellow and green vertices, they are reachable from each other without passing through any blue vertices
You are given an integer . Your task is to calculate the minimum number of vertices in a tree with exactly beautiful colorings.
Input
The first line contains a single integer — the number of test cases.
The only line of each test case contains a single integer .
Output
For each test case, print a single integer — the minimum number of vertices in a tree with exactly beautiful colorings. If such a tree does not exist, print .
Solution (Solution from Official Editorial)
Key Observation
Important: If a vertex is not green, its entire subtree must be colored the same color. For example, if the vertex is blue and it has a green or yellow descendant, the root is unreachable from that descendant without passing through that blue vertex. At the same time, the green vertices do not impose any additional constraints.
Counting Beautiful Colorings
Using this observation, let’s explore how to solve the inverse problem to the original one (namely, given a tree, count the number of beautiful colorings) using dynamic programming.
Let be the number of ways to color the subtree rooted at vertex , provided that itself is green. Since is green, each of its child vertices can be colored in any color:
- If a child is green, there are ways to color its subtree
- If is blue or yellow, there is only one way to color its subtree
Therefore, the total number of colorings for is .
Since each child subtree can be colored independently:
where are the children of .
Dynamic Programming Approach
Notice that the formula above shows the number of colorings for a tree depends solely on the number of colorings of its child subtrees. Therefore, we can solve the original problem using the following dynamic programming approach:
Let be the minimum number of vertices in a tree with exactly beautiful colorings.
To calculate this, we can use the fact that the number of colorings of a tree is the product of colorings of its child subtrees. Let’s iterate over the number of colorings of the last subtree (denote it ), then:
Tip
Think of it as having a tree root with green color that has colorings. Now we want to add another subtree to the root node of the tree with colorings. The new subtree has colorings with its root colored green. But this subtree can also be colored blue or yellow. Thus we have a new tree with colorings.
for all values of . Note that must be a divisor of .
Optimization
To speed up the solution, we can iterate through either up to or up to .
Time Complexity Analysis
This approach yields a solution with time complexity , where is the maximum number of colorings we need to consider. This is enough to get AC, but we can speed this up to as follows:
For every integer from to , let’s prepare the list of its divisors. We can iterate on the divisor and append it to the lists of divisors for integers
The integer will be processed in , so in total, we achieve a complexity of:
because it is the harmonic sum (multiplied by ).