Official

F - Colorful Star Editorial by evima


For \(N \le 2\) or \(M=1\), the problem can be easily solved, so we assume \(N \ge 3\) and \(M \ge 2\) from now on.

Hereafter, we call an edge whose endpoints have the same color a happy edge. Also, we define the depth of an edge as the depth of the farther endpoint from the root, and we call the set of vertices at depth \(k(\ge 1)\) a circle of depth \(k\).

To determine whether a certain tree can be created, we consider whether it is possible to return to the initial state by performing the reverse operation on the tree. The reverse operation is “select a pair of adjacent vertices with the same color and change the color of one of the vertices to any color.” Hereafter, when we refer to an operation, we mean this reverse operation. The conditions for a tree to be creatable are as follows:

  • If there are zero happy edges
  • It is always not creatable.
  • If there are one or two happy edges
  • First, use operations to bring the happy edge(s) to depth $1$. Then, the tree is creatable if one of the following conditions is met.
    • The vertices at depth $1$ have $N-2$ or fewer different colors.
    • The vertices at depth $1$ have $N-1$ different colors, and there exists a pair of vertices at depths $1$ and $2$ with the same color.

  • If there are \(3\) or more happy edges
  • It is always creatable.

The case with zero happy edges is trivial, and the case with three happy edges can be reduced to the case with one happy edge, or two happy edges and one of the conditions met. Therefore, we will prove the case with one or two happy edges.

First, use operations to bring the happy edge(s) to depth \(1\). We prove that it is impossible to create the tree when the conditions are not met.

If there are \(N\) different colors in the circle at depth \(1\), it is impossible to increase the number of happy edges by any operation, so the original tree cannot be created. Similarly, if there are \(N-1\) different colors at depth \(1\) and all vertices at depth \(2\) have the remaining color, it is clear that the number of happy edges cannot be increased.

We will show that if one of the two conditions is met, it is possible to change the color of a certain vertex at depth \(2\) or deeper to a target color and return to a state where one of the two conditions is met.

First, as the final goal, when the graph is divided into the root and \(N\) paths, we aim to make the following hold: “belonging to the same path \(\leftrightarrow\) having the same vertex color.” (If such a state is achieved, it is easy to swap colors between paths.)

When the former condition is met, it is easy to create a situation that meets the latter (or something stronger), so we assume the latter is met. In this case, using a pair of vertices at depths \(1\) and \(2\) with the same color, it is possible to create a path of length \(4\) where all vertices have the same color. Then, select one of the vertices \(v\) from a pair of vertices at depth \(2\) with a duplicated color. (If all colors are different, choose arbitrarily.) Change the colors so that the path of length \(4\) includes vertex \(v\). Then, change the color of the deepest vertex among the children of vertex \(v\) (including itself) not in the target color, and further put back the remaining path of length \(3\) to the root part. As a result, either the circle at depth \(1\) has \(N-2\) or fewer different colors, or it is possible to find a pair of vertices at depths \(1\) and \(2\) with the same color. (For the remaining vertex at depth \(2\), use one happy edge to align the color with the target color.)

Then, repeat this process until all vertices except for the circle at depth \(1\) are aligned with the target color. Now, make all colors in the circle at depth \(1\) different. Here, at the moment of changing the color of a vertex at depth \(2\) or deeper, change it to the same color as the current vertex at depth \(1\) in the same path to achieve the targeted structure. Therefore, the goal can be achieved if one of the above conditions is met.

What remains is to count the number of cases that meet the conditions. We will count the number of cases that do not meet them. The case with zero happy edges is simple, and the cases with one or two happy edges can be counted by considering the form after bringing the happy edge(s) to depth \(1\). The formulae in the count involve only powers and factorials, so this problem can be solved in \(\mathrm{O}(N + \log M)\).

Sample Implementation

posted:
last update: