Mrs. Worm owns a coal mine that can be represented as a network of nodes numbered from to . Node is the entrance to the mine, and for each other node there is a one-way tunnel going from a node to , with . 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:
Each mining shaft has exactly one escape tunnel going from to a node , in such a way that there is a path from to using only the original tunnels.
Each node has at most one escape tunnel that leads to it.
There is a path from each mining shaft to the entrance, if both the original tunnels and the escape tunnels can be used.
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 () representing the number of nodes in the coal mine. Each node is identified by a distinct integer from to .
The second line contains integers (), representing that there is a tunnel going from node to node .
Output a single line with integers , such that if node is a mining shaft, and 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.
2 1
0 1
3 1 1
*
8 1 2 3 4 4 2 7
0 0 0 0 1 3 0 2
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.