This page is still under construction.

Parts of this page are still being built. What you see may change.

Christmas Tree

Time limit0.7sMemory limit512 MB

Summary
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.
Level

Hard8 of 10

Topics
Tree, DFS, Greedy, Sorting
Solved
No attempts yet

Problem

Santa has a Christmas tree. Here a Christmas tree is a tree with NN 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 MM updates performed one after another. On update number XX the elf opens gift number XX (someone else's gift, of course) and finds a triplet (C,A,B)(C, A, B) inside. He then paints colour CC on every node of the chain that joins node AA and node BB. Every gift holds a different colour, so the MM colours are the MM distinct values from 1 to MM. 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 MM updates. Reconstruct MM triplets, in order, that turn the blank tree into the given one.

Input

The first line contains NN and MM.

The second line contains NN integers between 1 and MM. The XX-th of them is the colour of node XX.

Each of the next N−1N - 1 lines contains two integers AA and BB, meaning that node AA and node BB are joined by an edge.

The input satisfies the following constraints.

  • 1≤N,M≤1000001 \le N, M \le 100000
  • every colour of the final tree is between 1 and MM
  • at least one sequence of gifts produces the given tree
  • after all MM updates every node has been painted at least once

Output

Print MM lines. Line XX holds the triplet (C,A,B)(C, A, B) of gift number XX. 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 CC appears in the final tree, let PCP_C be the shortest chain that contains every node of colour CC. The input guarantee means this chain exists, and the shortest one is unique. Print colour CC as C a b, where aa is the smaller and bb is the larger of the two ends of PCP_C. When PCP_C is a single node, aa and bb are both that node.
  • If colour CC 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 CC has to be printed before colour DD whenever a node of colour DD lies on PCP_C. Among all orders that satisfy this condition, print the one whose sequence of colour numbers is lexicographically smallest.

Examples3

  1. Example 1

    Input
    6 3
    1 3 2 1 1 2
    1 4
    2 4
    3 4
    4 5
    5 6
    
    Expected output
    2 3 6
    1 1 5
    3 2 2
    
  2. Example 2

    Input
    5 3
    3 2 1 2 1
    1 2
    1 3
    1 4
    4 5
    
    Expected output
    1 3 5
    2 2 4
    3 1 1
    
  3. Example 3

    Input
    9 5
    1 2 3 4 5 4 3 2 1
    2 1
    3 2
    4 3
    5 4
    6 5
    7 6
    8 7
    9 8
    
    Expected output
    1 1 9
    2 2 8
    3 3 7
    4 4 6
    5 5 5