After each of n leaf insertions into a rooted tree, find the minimum number of vertex recolorings needed to restore the unique well coloring.
Hard8TreeDFSDynamic programmingGreedyNo attempts yetTime limit1sMemory limit1024 MBThe card game Magic: The Gathering has a rule for casting and countering spells. The coloring rule below comes from it. The card game itself is not explained here, and you do not need to know it to solve this problem.
For every rooted tree there is exactly one way to color the vertices black and white that satisfies this condition:
Induction proves that this coloring is unique. A tree colored this way is called well colored.
Start with a tree that consists of one black vertex, the root, and perform the following operation n times:
For each operation, find how many vertices are inverted.
The root is numbered 0, and the other vertices are numbered 1,2,…,n in the order they are added to the tree.
The first line contains one integer n, the number of vertex additions. (1≤n≤200000)
Each of the next n lines contains one integer vi, the parent of the vertex added in the i-th operation. Vertex vi already exists before the i-th operation, so vi<i.
Print n lines. The i-th line contains the number of vertices whose color is inverted during the i-th operation. The vertex attached by that operation arrives black and a leaf is always black, so it is never one of the inverted vertices.
The figure below shows the starting tree of the first example and the tree after each operation. The vertices inverted by that operation have a red outline.
