Mrs. Worm owns a coal mine that can be represented as a network of $$$N$$$ nodes numbered from $$$1$$$ to $$$N$$$. Node $$$1$$$ is the entrance to the mine, and for each other node $$$i$$$ there is a one-way tunnel going from a node $$$P_i$$$ to $$$i$$$, with $$$P_i \lt i $$$. Nodes that don't have any tunnels coming out of them are called mining shafts.
The Nlogonia government conducted a safety inspection of the mine and noted that, in case of an emergency, workers could be stuck inside the mining shafts, unable to escape. Mrs. Worm will certainly get sued unless she acts quickly!
To avoid this problem, Mrs. Worm decided to dig a set of one-way escape tunnels leading out from the mining shafts. This set must meet the following conditions:
Mrs. Worm needs your help! Find a valid set of escape tunnels that saves her from being sued or tell her that it is impossible to do so.
The first line contains an integer $$$N$$$ ($$$2 \leq N \leq 5 \times 10^{5}$$$) representing the number of nodes in the coal mine. Each node is identified by a distinct integer from $$$1$$$ to $$$N$$$.
The second line contains $$$N - 1$$$ integers $$${P_2}, {P_3}, \ldots, {P_N}$$$ ($$$1 \leq P_i \lt i$$$), representing that there is a tunnel going from node $$$P_i$$$ to node $$$i$$$.
Output a single line with $$$N$$$ integers $$${E_1}, {E_2}, \ldots, {E_N}$$$, such that $$$E_i=e(i)$$$ if node $$$i$$$ is a mining shaft, and $$$E_i=0$$$ otherwise. If there are multiple solutions, output any of them.
If there is no valid set of escape tunnels, output a single line with the character "*" (asterisk) instead.
21
0 1
31 1
*
81 2 3 4 4 2 7
0 0 0 0 1 3 0 2
Explanation for example 3
Nodes $$$5$$$, $$$6$$$ and $$$8$$$ are the mining shafts. The figure below shows four possible sets of escape tunnels for this test case. The first three sets are invalid, each one breaking one of the conditions. The last set is the proposed output $$$e(5) = 1$$$, $$$e(6) = 3$$$ and $$$e(8) = 2$$$, which is valid.
![]() | ![]() |
| (a) Invalid: $$$e(6) = 8$$$, but there is no path | (b) Invalid: two escape tunnels lead to node $$$1$$$. |
| from node $$$8$$$ to node $$$6$$$ using only the original tunnels. | |
![]() | ![]() |
| (c) Invalid: mining shaft $$$8$$$ cannot reach the entrance. | (d) Valid: the set of escape tunnels of the output. |
| Name |
|---|


