#P9600. [IOI 2023] 封锁时刻

[IOI 2023] 封锁时刻

Background

This is an interactive problem. You only need to implement the function required in the code.

Your code does not need to include any additional header files, and you do not need to implement the main function.

This problem only supports submissions in C++.

Problem Description

There are NN cities in Hungary, numbered from 00 to N1N - 1.

These cities are connected by N1N - 1 bidirectional roads, numbered from 00 to N2N - 2. For each jj (0jN20 \le j \le N - 2), road jj connects city U[j]U[j] and city V[j]V[j], and has length W[j]W[j], meaning the travel time between these two cities is W[j]W[j] time units. Each road connects two different cities, and between any two cities there is at most one road.

A path between two different cities aa and bb is a sequence of distinct cities p0,p1,,ptp_0, p_1, \ldots, p_t satisfying:

  • p0=ap_0 = a,
  • pt=bp_t = b,
  • for each ii (0i<t0 \le i \lt t), there is a road connecting pip_i and pi+1p_{i + 1}.

Using these roads, it is possible to travel from any city to any other city. In other words, there is a path between any two different cities.
It can be proven that the path between any two different cities is unique.

The length of a path p0,p1,,ptp_0, p_1, \ldots, p_t is the sum of the lengths of the tt roads connecting consecutive cities along the path.

In Hungary, many people attend celebrations held in two major cities on National Day. When the celebrations end, they go home. To prevent crowds from disturbing local residents, the government decided to block cities at certain moments. Each city is assigned a non-negative blockade moment. The government decided that the sum of blockade moments of all cities must not exceed KK. Specifically, for each ii (0iN10 \leq i \leq N - 1), the blockade moment assigned to city ii is a non-negative integer c[i]c[i]. The sum of all c[i]c[i] does not exceed KK.

Consider a city aa and an assignment of blockade moments. We say that a city bb is reachable from city aa if and only if at least one of the following two cases holds.

Case 1: b=ab = a.

Case 2: The path p0,,ptp_0, \ldots, p_t between the two cities (p0=ap_0 = a and pt=bp_t = b) satisfies:

  • the length of the path p0,p1p_0, p_1 is at most c[p1]c[p_1], and
  • the length of the path p0,p1,p2p_0, p_1, p_2 is at most c[p2]c[p_2], and
  • \ldots
  • the length of the path p0,p1,p2,,ptp_0, p_1, p_2, \ldots, p_t is at most c[pt]c[p_t].

This year, the two main celebration locations are in cities XX and YY.
For each assignment of blockade moments, we define a convenience score as the sum of the following two numbers:

  • the number of cities reachable from city XX,
  • the number of cities reachable from city YY.

Note that if a city is reachable from both city XX and city YY, then it is counted twice when computing the convenience score.

Your task is to compute the maximum convenience score that can be achieved by some assignment of blockade moments.

Input Format

Let CC be the number of scenarios, i.e. the number of calls to max_score. The grader reads the input in the following format:

  • Line 11: CC

The following are the descriptions of the CC scenarios.

The grader reads the description of each scenario in the following format:

  • Line 11: N  X  Y  KN \; X \; Y \; K
  • Line 2+j2 + j (0jN20 \le j \le N - 2): U[j]  V[j]  W[j]U[j] \; V[j] \; W[j]

Output Format

For each scenario, the grader prints a separate line in the following format:

  • Line 11: the return value of max_score
2
7 0 2 10
0 1 2
0 3 3
1 2 4
2 4 2
2 5 5
5 6 3
4 0 3 20
0 1 18
1 2 1
2 3 19

6
3

Hint

Implementation Details

You need to implement the following function.

int max_score(int N, int X, int Y, int64 K, int[] U, int[] V, int[] W)
  • NN: the number of cities.
  • XX, YY: the two major celebration cities.
  • KK: the upper bound on the total sum of blockade moments.
  • UU, VV: arrays of length N1N - 1 describing the road connections.
  • WW: an array of length N1N - 1 describing the road lengths.
  • The function should return the maximum convenience score achievable by some assignment of blockade moments.
  • This function may be called multiple times for each test case.

Examples

Consider the following call:

max_score(7, 0, 2, 10,
          [0, 0, 1, 2, 2, 5], [1, 3, 2, 4, 5, 6], [2, 3, 4, 2, 5, 3])

This corresponds to the following road network:

Suppose the blockade moments are assigned as follows:

City 00 11 22 33 44 55 66
Blockade moment 00 44 00 33 22 00

Note that the sum of all blockade moments is 99, which does not exceed K=10K = 10. Cities 00, 11, and 33 are reachable from city XX (X=0X = 0), and cities 11, 22, and 44 are reachable from city YY (Y=2Y = 2). Therefore, the convenience score is 3+3=63 + 3 = 6. There is no assignment of blockade moments that makes the convenience score greater than 66, so the function should return 66.

Consider another call:

max_score(4, 0, 3, 20, [0, 1, 2], [1, 2, 3], [18, 1, 19])

This corresponds to the following road network:

Suppose the blockade moments are assigned as follows:

City 00 11 22 33
Blockade moment 00 11 1919 00

City 00 is reachable from city XX (X=0X = 0), and cities 22 and 33 are reachable from city YY (Y=3Y = 3). Therefore, the convenience score is 1+2=31 + 2 = 3. There is no assignment of blockade moments that makes the convenience score greater than 33, so the function should return 33.

Constraints

  • 2N2000002 \le N \le 200\,000
  • 0X<Y<N0 \le X \lt Y \lt N
  • 0K10180 \le K \le 10^{18}
  • 0U[j]<V[j]<N0 \le U[j] \lt V[j] \lt N (for each jj with 0jN20 \le j \le N - 2)
  • 1W[j]1061 \le W[j] \le 10^6 (for each jj with 0jN20 \le j \le N - 2)
  • Using these roads, you can travel from any city to any other city.
  • SN200000S_N \le 200\,000, where SNS_N is the sum of NN over all calls to max_score.

Subtasks

We say a road network is linear if road ii connects city ii and city i+1i + 1 (for each ii with 0iN20 \le i \le N - 2).

  1. (8 points) The length of the path from city XX to city YY is greater than 2K2K.
  2. (9 points) SN50S_N \le 50, and the road network is linear.
  3. (12 points) SN500S_N \le 500, and the road network is linear.
  4. (14 points) SN3000S_N \le 3\,000, and the road network is linear.
  5. (9 points) SN20S_N \le 20.
  6. (11 points) SN100S_N \le 100.
  7. (10 points) SN500S_N \le 500.
  8. (10 points) SN3000S_N \le 3\,000.
  9. (17 points) No additional constraints.

Translated by ChatGPT 5