The yearly Multi-University Training Contest is coming, and ?? had already prepared an interesting and challenging problem for all those intelligent brains. However, when he told teammates about his “wonderful idea”, it really shocked him that all of them, except himself, had already seen an existing problem with the same trick.So he need to rack his brain again.
But nothing is difficult for a willing heart. After thinking about it all through the night, he came up with a new problem:
The original rooted tree has n nodes, numbered from 1 to n. Each node has a value, all of these values form a permutation of [1,n]. We reset these values by the following way:
new value(P) = max{previous value(Q)|Q is in the subtree with root P}
Given the new tree(each node’s number, it’s neighbors and it’s value), your job is to restore its original appearance.
Note that the root of the given tree is unspecified.