Add on a Tree: Revolution
Contest: Codeforces Round 572 (Div. 1)
Difficulty rating: 2500
Tags: constructive algorithms, dfs and similar, implementation, trees

Note that this is the second problem of the two similar problems. You can hack this problem if you solve it. But you can hack the previous problem only if you solve both problems.

You are given a tree with $$$n$$$ nodes. In the beginning, $$$0$$$ is written on all edges. In one operation, you can choose any $$$2$$$ distinct leaves $$$u$$$, $$$v$$$ and any integer number $$$x$$$ and add $$$x$$$ to values written on all edges on the simple path between $$$u$$$ and $$$v$$$. Note that in previous subtask $$$x$$$ was allowed to be any real, here it has to be integer.

For example, on the picture below you can see the result of applying two operations to the graph: adding $$$2$$$ on the path from $$$7$$$ to $$$6$$$, and then adding $$$-1$$$ on the path from $$$4$$$ to $$$5$$$.

You are given some configuration of nonnegative integer pairwise different even numbers, written on the edges. For a given configuration determine if it is possible to achieve it with these operations, and, if it is possible, output the sequence of operations that leads to the given configuration. Constraints on the operations are listed in the output format section.

Leave is a node of a tree of degree $$$1$$$. Simple path is a path that doesn't contain any node twice.

Input format:
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 1000$$$) — the number of nodes in a tree.

Each of the next $$$n-1$$$ lines contains three integers $$$u$$$, $$$v$$$, $$$val$$$ ($$$1 \le u, v \le n$$$, $$$u \neq v$$$, $$$0 \le val \le 10\,000$$$), meaning that there is an edge between nodes $$$u$$$ and $$$v$$$ with $$$val$$$ written on it. It is guaranteed that these edges form a tree. It is guaranteed that all $$$val$$$ numbers are pairwise different and even.

Output format:
If there aren't any sequences of operations which lead to the given configuration, output "NO".

If it exists, output "YES" in the first line. In the second line output $$$m$$$ — number of operations you are going to apply ($$$0 \le m \le 10^5$$$). Note that you don't have to minimize the number of the operations!

In the next $$$m$$$ lines output the operations in the following format:

$$$u, v, x$$$ ($$$1 \le u, v \le n$$$, $$$u \not = v$$$, $$$x$$$ — integer, $$$-10^9 \le x \le 10^9$$$), where $$$u, v$$$ — leaves, $$$x$$$ — number we are adding.

It is guaranteed that if there exists a sequence of operations producing given configuration, then there exists a sequence of operations producing given configuration, satisfying all the conditions above.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
The configuration from the first sample is drawn below, and it is impossible to achieve.

The sequence of operations from the second sample is illustrated below.

Example input:
5
1 2 2
2 3 4
3 4 10
3 5 18

Example output:
NO

Example input:
6
1 2 6
1 3 8
1 4 12
2 5 2
2 6 4

Example output:
YES
4
3 6 1
4 6 3
3 4 7
4 5 2

Editorial:
Because all numbers are different, in the second subtask if we have a vertex with degree $$$2$$$ then answer is NO. If there is no such then construction also follows from proof. Indeed, if we can add on any path to leaf, then we can add on one edge. So, consider any edge $$$uv$$$ and suppose we want to add $$$x$$$ on this edge. Let's find any leaf in a subtree of vertex $$$u$$$, which doesn't contain $$$v$$$, let's name it $$$l$$$. If $$$l = u$$$, just add $$$x$$$ on path $$$uv$$$. Else, add $$$x$$$ on path $$$vl$$$ and $$$−x$$$ on path $$$ul$$$. It's clear, that after this two operations we've added $$$x$$$ on edge $$$uv$$$ and didn't add anything on other edges. Then, just add on each edge needed number.
In the end, let's talk about implementation. To add on the path to leaf it's sufficient to find a leaf in the subtree. We can do it naively in $$$O(n)$$$, then complexity is $$$O(n^2)$$$. Also, we can precalculate leaves in each subtree and, for example, root tree at some leaf. Then, it's possible to do all operations in $$$O(1)$$$, and complexity is $$$O(n)$$$, but it wasn't needed.
$$$\textbf{Challenge}$$$
Solve task if numbers are not necessary even and different, but all operations should be also with integer $$$x$$$(now it turns out that sometimes it's possible to solve it in rationals, but not in integers).
// code for 1 subtask
// code for 2 subtask