Tree Sudoku
Time limit1sMemory limit1024 MB
Assign each vertex of a tree a distinct value from sequence A so that all adjacent-pair sums are pairwise distinct, or report NO.
- Level
Medium7 of 10
- Topics
- Tree, Greedy, Implementation
- Solved
- No attempts yet
Problem
Lulu, tired of ordinary Sudoku, proposed a new kind of Sudoku.
- A tree with vertices and a sequence of integers are given. The -th element of sequence is , and all elements of are distinct.
- Match every vertex of the tree with exactly one element of the sequence. That is, each vertex is matched to one element of the sequence, and no two vertices are matched to the same element.
- Each vertex takes the value of its matched element. The sums of the values of every pair of adjacent vertices must all be different. In other words, the sum of any pair of adjacent vertices must differ from the sum of every other pair of adjacent vertices.
Input
The first line contains an integer . ()
The second line contains the sequence , separated by spaces. (, all elements of are distinct integers.)
The next lines contain the edges of the tree in the form a b. This means there is an edge connecting vertex and vertex . ()
Output
On the first line, print YES if there is a way to match the vertices and the sequence elements so that the sums of values of adjacent vertices are all different. Otherwise, print NO.
If such a way exists, print on the next line the index of the sequence element matched to each vertex, in order of vertex number, separated by spaces.
If there are several valid ways, print any of them.