Truck Driver

시간 제한4.5초메모리 제한1024 MB

요약
가중치가 있는 트리에서 각 도시마다 배달 횟수가 정해져 있고, 하루마다 한 도시의 횟수가 바뀔 때 도시 0에서 출발해 도시 i를 정확히 W[i]번 방문하고 돌아오는 닫힌 경로의 최대 이동 시간을 구한다.
난이도

어려움10점 중 8점

유형
트리, 그리디, 동적 계획법, 구현
정답자
아직 제출이 없습니다

문제

Hungary is a country with NN cities numbered from 00 to N−1N - 1.

The cities are connected by N−1N - 1 bidirectional roads, numbered from 00 to N−2N - 2. Road jj (0≤j≤N−20 ≤ j ≤ N - 2) connects city U\[j]U\[j] and city V\[j]V\[j] and has length T\[j]T\[j], that is, it allows one to travel between the cities in T\[j]T\[j] units of time. Each road connects two different cities, and each pair of cities is connected by at most one road.

A path between two distinct cities aa and bb is a sequence of distinct cities p_0,p_1,…,p_lp\_0 , p\_1 , \dots , p\_l such that:

  • p_0=ap\_0 = a,
  • p_l=bp\_l = b,
  • for each ii (0≤i<l0 ≤ i < l), there is a road connecting cities p_ip\_i and p_i+1p\_{i+1}.

It is possible to travel from any city to any other city by using the roads, that is, there is a path between every two distinct cities. Note that the path connecting any pair of cities is unique.

The distance of cities aa and bb is notated by d(a,b)d(a, b) and defined as follows:

  • if a=ba = b then d(a,b)=0d(a, b) = 0,
  • otherwise d(a,b)d(a, b) is the total length of the roads connecting consecutive cities in the path between aa and bb.

Karcsi is a truck driver who has to complete some number of deliveries in the cities. For each ii from 00 to N−1N - 1, inclusive, Karcsi has to complete W\[i]W\[i] deliveries in city ii. Karcsi starts from city 00, and he is allowed to complete the deliveries in any order, after which he returns to city 00. A delivery plan is a (possibly empty) sequence of cities, c_1,c_2,…,c_mc\_1 , c\_2 ,\dots , c\_m, such that for each ii (0≤i<N0 ≤ i < N) the sequence contains city ii exactly W\[i]W\[i] times.

The delivery time of a plan c_1,c_2,…,c_mc\_1 , c\_2 , \dots ,c\_m is the sum of the distances of consecutive cities in the sequence 0,c_1,c_2,…,c_m,00, c\_1 , c\_2 , \dots ,c\_m, 0, that is, d(0,c_1)+d(c_1,c_2)+⋯+d(c_m,0)d(0, c\_1 ) + d(c\_1 , c\_2 ) + \cdots + d(c\_m, 0).

Karcsi has to work for QQ days. At the start of each day, the number of required deliveries changes in one of the cities. For some city SS and nonnegative integer XX, the value of W\[S]W\[S] becomes XX. The value of W\[S]W\[S] remains XX as long as it is not modified again at the start of a day later on.

Karcsi gets paid by the hour. He wants to choose a delivery plan so that the delivery time is the maximum over all possible plans. Your task is to compute the maximum delivery time for each day when Karcsi has to work.

제한

  • 2≤N≤100,0002 ≤ N ≤ 100\\,000
  • 0≤U\[j]<V\[j]<N0 ≤ U\[j] < V\[j] < N (for each jj such that 0≤j≤N−20 ≤ j ≤ N - 2)
  • 1≤T\[j]≤1001 ≤ T\[j] ≤ 100 (for each jj such that 0≤j≤N−20 ≤ j ≤ N - 2)
  • It is possible to travel from any city to any other city by using the roads.
  • 0≤W\[i]≤1060 ≤ W\[i] ≤ 10^6 (for each ii such that 0≤i<N0 ≤ i < N)
  • 1≤Q≤300,0001 ≤ Q ≤ 300\\,000
  • 0≤S<N0 ≤ S < N
  • 0≤X≤106 0 ≤ X ≤ 10^6

예제

이 문제는 공개된 예제가 없습니다.