| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 4.5 초 | 1024 MB | 2 | 2 | 2 | 100.000% |
Hungary is a country with $N$ cities numbered from $0$ to $N - 1$.
The cities are connected by $N - 1$ bidirectional roads, numbered from $0$ to $N - 2$. Road $j$ ($0 ≤ j ≤ N - 2$) connects city $U[j]$ and city $V[j]$ and has length $T[j]$, that is, it allows one to travel between the cities in $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 $a$ and $b$ is a sequence of distinct cities $p_0 , p_1 , \dots , p_l$ such that:
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 $a$ and $b$ is notated by $d(a, b)$ and defined as follows:
Karcsi is a truck driver who has to complete some number of deliveries in the cities. For each $i$ from $0$ to $N - 1$, inclusive, Karcsi has to complete $W[i]$ deliveries in city $i$. Karcsi starts from city $0$, and he is allowed to complete the deliveries in any order, after which he returns to city $0$. A delivery plan is a (possibly empty) sequence of cities, $c_1 , c_2 ,\dots , c_m$, such that for each $i$ ($0 ≤ i < N$) the sequence contains city $i$ exactly $W[i]$ times.
The delivery time of a plan $c_1 , c_2 , \dots ,c_m$ is the sum of the distances of consecutive cities in the sequence $0, c_1 , c_2 , \dots ,c_m, 0$, that is, $d(0, c_1 ) + d(c_1 , c_2 ) + \cdots + d(c_m, 0)$.
Karcsi has to work for $Q$ days. At the start of each day, the number of required deliveries changes in one of the cities. For some city $S$ and nonnegative integer $X$, the value of $W[S]$ becomes $X$. The value of $W[S]$ remains $X$ 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.
Your task is to implement two procedures.
void init(int N, int[] U, int[] V, int[] T, int[] W)
max_time.int64 max_time(int S, int X)
Consider the following sequence of calls:
init(5, [0, 0, 1, 1], [1, 2, 3, 4], [1, 2, 3, 1], [0, 0, 1, 0, 1])
The parameters correspond to the road network below. Red numbers represent the initial number of deliveries in each city.
max_time(0, 1)
After the update, we have $W = [1, 0, 1, 0, 1]$. A possible delivery plan is the sequence $4, 2, 0$. In this case, Karcsi visits cities $0, 4, 2, 0, 0$ in this order, and the delivery time is $d(0, 4) + d(4, 2) + d(2, 0) + d(0, 0) = 2 + 4 + 2 + 0 = 8$.
There is no delivery plan with a delivery time greater than 8, thus the procedure should return $8$.
Further examples of calls to max_time are summarized in the following table.
| Call | $W$ after the update | Optimal plan | Maximum delivery time |
|---|---|---|---|
max_time(3, 3) |
$[1, 0, 1, 3, 1]$ | $3, 0, 3, 2, 3, 4$ | $4 + 4 + 4 + 6 + 6 + 4 + 2 = 30$ |
max_time(0, 0) |
$[0, 0, 1, 3, 1]$ | $3, 2, 3, 4, 3$ | $4 + 6 + 6 + 4 + 4 + 4 = 28$ |
max_time(4, 0) |
$[0, 0, 1, 3, 0]$ | $3, 2, 3, 3$ | $4 + 6 + 6 + 0 + 4 = 20$ |
max_time(2, 0) |
$[0, 0, 0, 3, 0]$ | $3, 3, 3$ | $4 + 0 + 0 + 4 = 8$ |
max_time(3, 0) |
$ [0, 0, 0, 0, 0]$ | empty | $0$ |
| 번호 | 배점 | 제한 |
|---|---|---|
| 1 | 8 | $N = 2$ |
| 2 | 21 | $N,Q ≤ 1\,000$ |
| 3 | 14 | $U[j] = \lfloor \frac{j}{2} \rfloor$ and $V[j] = j + 1$ for each $j$ such that $0 ≤ j ≤ N - 2$ |
| 4 | 21 | $U[j] = j$ and $V[j] = j + 1$ for each $j$ such that $0 ≤ j ≤ N - 2$ |
| 5 | 36 | No additional constraints. |
The sample grader reads the input in the following format:
The sample grader prints your answers in the following format:
max_time after update $k$C++17, C++20, C++17 (Clang), C++20 (Clang)