CF1943C Tree Compass
Description
You are given a tree with $ n $ vertices numbered $ 1, 2, \ldots, n $ . Initially, all vertices are colored white.
You can perform the following two-step operation:
1. Choose a vertex $ v $ ( $ 1 \leq v \leq n $ ) and a distance $ d $ ( $ 0 \leq d \leq n-1 $ ).
2. For all vertices $ u $ ( $ 1 \leq u \leq n $ ) such that $ \text{dist}^\dagger(u,v)=d $ , color $ u $ black.
Construct a sequence of operations to color all the nodes in the tree black using the minimum possible number of operations. It can be proven that it is always possible to do so using at most $ n $ operations.
$ ^\dagger $ $ \text{dist}(x, y) $ denotes the number of edges on the (unique) simple path between vertices $ x $ and $ y $ on the tree.
Input Format
N/A
Output Format
N/A
Explanation/Hint
In the first test case, there is only one possible operation, and performing it gives us a valid answer.
In the second test case, the first operation colors vertex $ 2 $ black, and the second operation colors vertex $ 1 $ black. It can be shown that it is impossible to color both vertices black in one operation, so the minimum number of operations needed is $ 2 $ . Another possible solution is to use the $ 2 $ operations: $ (u, r) = (1, 0) $ and $ (u, r) = (2, 0) $ .
In the third test case, the first operation colors vertices $ 2 $ , $ 3 $ and $ 4 $ black, and the second operation colors vertex $ 1 $ black. Again, it can be shown that it is impossible to color all vertices black in $ 1 $ operation, so the minimum number of operations needed is $ 2 $ .
In the fourth test case, the first operation colors vertices $ 4 $ , $ 1 $ and $ 7 $ black, the second operation colors vertices $ 2 $ , $ 5 $ and $ 6 $ black while the third operation colors vertices $ 3 $ and $ 7 $ black. Notice that it is allowed to color vertex $ 7 $ black twice.
Thus, each node was marked at least once, with node $ 7 $ marked twice. It can be shown that it is impossible to color all vertices black in fewer than $ 3 $ moves.