#P9175. [COCI 2022/2023 #4] Mreža

    ID: 10128 远端评测题 5000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2022可持久化线段树COCI(克罗地亚)

[COCI 2022/2023 #4] Mreža

Background

Account suspension due to “carding” in the judge.

Problem Description

Mayor Mirko lives in a city with nn neighborhoods. These nn neighborhoods are connected by n1n - 1 two-way roads, and from any neighborhood you can reach any other neighborhood.

Mirko wants to upgrade some roads to ease traffic. For each road, we know the current driving speed viv_i on this road, the upgrade cost cic_i, and the driving speed sis_i after upgrading.

There are qq unhappy citizens who come to see Mirko. Each of them has their own upgrade suggestion. The ii-th citizen suggests: “We should invest eie_i euros to upgrade the roads on the route between neighborhoods aia_i and bib_i.”

For each suggestion, Mirko is interested in the following: if his goal is to maximize the minimum driving speed between neighborhoods aia_i and bib_i, then what is that minimum driving speed when he can spend at most eie_i euros on upgrading roads.

Mirko immediately realizes that this problem is not simple, so he hires you to help him.

Input Format

The first line contains an integer n (2n105)n\ (2\le n\le 10^5), the total number of neighborhoods.

The next n1n - 1 lines each contain 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\le 10^9,1\le c_i\le 10^9)$, meaning there is a road between neighborhoods xix_i and yiy_i. The current driving speed on this road is viv_i, the upgrade cost is cic_i, and the driving speed after upgrading is sis_i.

The next line contains an integer q (1q105)q\ (1\le q\le 10^5), the total number of unhappy citizens.

The next qq lines each contain three integers $a_i,b_i,e_i\ (1\le a_i,b_i\le n,a_i\neq b_i,1\le e_i\le 10^{18})$, with the meaning as described above.

Output Format

Output qq lines, where each line is the answer for the corresponding citizen’s suggestion.

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
7
5
11
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
6
7
5
7

Hint

Sample explanation 11: The figure below shows the city and the neighborhoods. On each edge, the numbers are the current driving speed, the upgrade cost, and the driving speed after upgrading.

If we upgrade the roads between 11 and 22, and between 11 and 33, then the driving speeds from 22 to 44 will become 10,9,710,9,7. The minimum is 77.

If we upgrade the road between 44 and 33, then the driving speeds from 66 to 44 will become 5,155,15. The minimum is 55.

If we upgrade the road between 33 and 55, then the driving speed from 55 to 33 will become 1111.

Subtask ID Additional Constraints Score
00 Sample only 00
11 n,q1000n,q\le 1000 1919
22 Each neighborhood is connected to at most two other neighborhoods 2626
33 No additional constraints 5555

Translated by ChatGPT 5