Berland Beauty
Contest: Codeforces Round 617 (Div. 3)
Difficulty rating: 2100
Tags: constructive algorithms, dfs and similar, greedy, sortings, trees

There are $$$n$$$ railway stations in Berland. They are connected to each other by $$$n-1$$$ railway sections. The railway network is connected, i.e. can be represented as an undirected tree.

You have a map of that network, so for each railway section you know which stations it connects.

Each of the $$$n-1$$$ sections has some integer value of the scenery beauty. However, these values are not marked on the map and you don't know them. All these values are from $$$1$$$ to $$$10^6$$$ inclusive.

You asked $$$m$$$ passengers some questions: the $$$j$$$-th one told you three values:

- his departure station $$$a_j$$$;
- his arrival station $$$b_j$$$;
- minimum scenery beauty along the path from $$$a_j$$$ to $$$b_j$$$ (the train is moving along the shortest path from $$$a_j$$$ to $$$b_j$$$).

You are planning to update the map and set some value $$$f_i$$$ on each railway section — the scenery beauty. The passengers' answers should be consistent with these values.

Print any valid set of values $$$f_1, f_2, \dots, f_{n-1}$$$, which the passengers' answer is consistent with or report that it doesn't exist.

Input format:
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5000$$$) — the number of railway stations in Berland.

The next $$$n-1$$$ lines contain descriptions of the railway sections: the $$$i$$$-th section description is two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i, y_i \le n, x_i \ne y_i$$$), where $$$x_i$$$ and $$$y_i$$$ are the indices of the stations which are connected by the $$$i$$$-th railway section. All the railway sections are bidirected. Each station can be reached from any other station by the railway.

The next line contains a single integer $$$m$$$ ($$$1 \le m \le 5000$$$) — the number of passengers which were asked questions. Then $$$m$$$ lines follow, the $$$j$$$-th line contains three integers $$$a_j$$$, $$$b_j$$$ and $$$g_j$$$ ($$$1 \le a_j, b_j \le n$$$; $$$a_j \ne b_j$$$; $$$1 \le g_j \le 10^6$$$) — the departure station, the arrival station and the minimum scenery beauty along his path.

Output format:
If there is no answer then print a single integer -1.

Otherwise, print $$$n-1$$$ integers $$$f_1, f_2, \dots, f_{n-1}$$$ ($$$1 \le f_i \le 10^6$$$), where $$$f_i$$$ is some valid scenery beauty along the $$$i$$$-th railway section.

If there are multiple answers, you can print any of them.

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

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

Example output:
5 3 5

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

Example output:
5 3 1 2 1

Editorial:
Firstly, let's precalculate $$$n$$$ arrays $$$p_1, p_2, \dots, p_n$$$. The array $$$p_v$$$ is the array of "parents" if we run dfs from the vertex $$$v$$$. So, $$$p_{v, u}$$$ is the vertex that is the previous one before $$$u$$$ on the directed path $$$(v, u)$$$. This part can be precalculated in time $$$O(n^2)$$$ and we need it just for convenience.
Initially, all values $$$f_j$$$ (beauties of the edges) are zeros. Let's consider queries in order of non-decreasing $$$g_i$$$. For the current query, let's consider the whole path $$$(a_i, b_i)$$$ and update the value $$$f_j$$$ for each $$$j$$$ on this path in the following way: $$$f_j = max(f_j, g_i)$$$. After processing all queries, let's replace all values $$$f_j = 0$$$ with $$$f_j = 10^6$$$. This part works also in time $$$O(n^2)$$$.
And the last part of the solution is to check if the data we constructed isn't contradictory. We can iterate over all paths $$$(a_i, b_i)$$$ and find the minimum value $$$f_j$$$ on this path. We have to sure if it equals $$$g_i$$$. If it isn't true for at least one query, then the answer is -1.
Otherwise, we can print the resulting tree.
Time complexity: $$$O(n^2)$$$, but it can be done in at least $$$O(n \log n)$$$ (I hope someone can explain this solution because I am too lazy to do it now).