You are given an undirected tree with nodes labeled through . Each node has an integer value b_u assigned to it. Your task is to orient (assign directions to) each edge of the tree so that, for every node , the following condition holds:
where:
is the number of edges directed away from ,
is the number of edges directed toward .
It is guaranteed that: |b_u| \le \deg(u), and \sum_{u=1}^n b_u = 0 where is the degree of in the undirected tree.
If there exists an orientation of edges that satisfies the above conditions, print YES followed by any valid orientation. Otherwise, print NO.
The first line contains an integer (1 \le n \le 2 \cdot 10^5) — the number of nodes.
The second line contains integers b_1, b_2, \dots, b_n (|b_u| \le \deg(u), and \sum_{u=1}^n b_u = 0).
The next n-1 lines each contain two integers and (1 \le x, y \le n, x \neq y), denoting an undirected edge between nodes and . It is guaranteed that these edges form a tree.
If a valid orientation exists, print:
The word YES on the first line.
Then n-1 lines, each containing two integers — indicating a directed edge from to .
If no valid orientation exists, print NO.
Example 1
Example 2
For the first testcase:
Subtask ( points): (n \le 16)
Subtask ( points): (n \le 1000)
Subtask ( points): Tree is a simple path
Subtask ( points): Tree is a star
Subtask ( points): No additional constraints.