Christmas Tree
Time limit0.7sMemory limit512 MB
Given the final colours on a tree after M path-painting updates with distinct colours, reconstruct the unique valid update order and the endpoints of each colour's shortest covering path.
Problem
Santa has a Christmas tree. Here a Christmas tree is a tree with nodes, and every node carries one colour. At the start every node has colour 0.
One night an elf decided the tree was boring and repainted it with updates performed one after another. On update number the elf opens gift number (someone else's gift, of course) and finds a triplet inside. He then paints colour on every node of the chain that joins node and node . Every gift holds a different colour, so the colours are the distinct values from 1 to . A node that is already painted is simply repainted, and its old colour is gone forever.
The triplets are lost. All that is left is the final tree: you are given the colour of every node after all updates. Reconstruct triplets, in order, that turn the blank tree into the given one.
Input
The first line contains and .
The second line contains integers between 1 and . The -th of them is the colour of node .
Each of the next lines contains two integers and , meaning that node and node are joined by an edge.
The input satisfies the following constraints.
- every colour of the final tree is between 1 and
- at least one sequence of gifts produces the given tree
- after all updates every node has been painted at least once
Output
Print lines. Line holds the triplet of gift number . The elf paints in the order you print, so the order matters.
Many sequences of gifts can produce the same tree, so print the one that the rules below single out.
- If colour appears in the final tree, let be the shortest chain that contains every node of colour . The input guarantee means this chain exists, and the shortest one is unique. Print colour as
C a b, where is the smaller and is the larger of the two ends of . When is a single node, and are both that node. - If colour does not appear in the final tree, print it as
C 1 1. - The colours that do not appear come first, in increasing order of colour number.
- The colours that appear come after them. Colour has to be printed before colour whenever a node of colour lies on . Among all orders that satisfy this condition, print the one whose sequence of colour numbers is lexicographically smallest.