Problem list
Log in
#coci00643 · COCI 2022/2023 Contest 4

Mreža

Diamond V 5.0 s 512 MB

The city mayor Mirko lives in a city with $n$ neighborhoods connected with $n - 1$ bidirectional roads such that from any neighborhood it is possible to reach every other neighborhood. Mirko wants to upgrade some roads to reduce traffic. For every road, we know the current speed $v_{i}$ vehicles drive on it, the price of upgrading $c_{i}$ and the speed of driving after upgrading $s_{i}$. There are $q$ unsatisfied citizens that come to visit Mirko. Each one has their suggestion for an upgrade. The suggestion of the $i$-th citizen is: “We should invest $e_{i}$ euros in upgrading roads between neighborhoods $a_{i}$ and $b_{i}$” For each suggestion, Mirko is interested in what is the minimum driving speed between neighborhoods $a_{i}$ and $b_{i}$ if he spends at most $e_{i}$ euros on upgrading the roads, given thathis goal is to maximize the minimum driving speed between the neighborhoods $a_{i}$ and $b_{i}$. Mirko soon realized that calculating this is not an easy task and hired you to help him!

Input

The first line contains the integer $n$ ($2 \le n \le 100$, 000), the number of neighborhoods. In each of the next $n - 1$ lines there are five integers $x_{i}$, $y_{i}$, $v_{i}$, $c_{i}$, $s_{i}$ ($1 \le x_{i}$, $y_{i} \le n$, $1 \le v_{i} < s_{i}$ ≤ $10^{9}$, $1 \le c_{i} \le 10^{9}$), denoting that neighborhood $x_{i}$ and $y_{i}$ are connected, current driving speed is $v_{i}$, cost of upgrading the road is $c_{i}$, and the speed on the road would be $s_{i}$. The next line contains the integer $q$ ($1 \le q \le 100,000$), the number of unsatisfied citizens. In each of the next $q$ lines there are three integers $a_{i}$, $b_{i}$, $e_{i}$ ($1 \le a_{i}$, $b_{i} \le n$, $a_{i}$̸ = $b_{i}$, $1 \le e_{i} \le 10^{18}$), which describe the suggestion of the $i$-th citizen.

Output

In the $i$-th of the $q$ lines print the answer to the request of the $i$-th citizen.

Example 1

Input
6
1 2 5 7 10
1 3 4 8 9
3 4 7 1 15
3 5 6 3 11
3 6 5 6 8
3
2 4 15
6 4 5
3 5 10
Output
7
5
11

Example 2

Input
4
1 2 5 5 8
2 3 4 6 9
3 4 6 10 7
4
1 4 16
2 4 16
1 4 10
3 4 10
Output
6
7
5
7

Clarification of the first example: The illustration represents the city and its neighborhoods. On the edges are written the current driving speed, the cost of upgrading, and the speed after upgrading, respectively. If we upgrade the roads between 1 and 2, and between 1 and 3, the driving speeds from 2 to 4 will be 10, 9, and 7 m/s. The minimum is 7 m/s. If we upgrade the roads between 4 and 3, the driving speeds from 6 to 4 will be 5 an 15 m/s. The minimum is 5 m/s. If we upgrade the road between 3 and 5, the driving speed from 5 to 3 will be 11 m/s.

Source
COCI 2022/2023 Contest 4
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code