#P16452. [APIO 2026] APIOBike

    ID: 16438 Type: RemoteJudge 3000ms 2048MiB Tried: 0 Accepted: 0 Difficulty: 9 Uploaded By: Tags>APIO交互题Special Judge2026

[APIO 2026] APIOBike

题目描述

APIOBike is a newly launched bike-sharing service in APIO City. Several stations have been set up throughout the city, allowing users to borrow or return bikes at any station. Due to its convenience and low cost, it has quickly become the most important mode of transportation in APIO City. However, APIOBike has encountered a common problem faced by all bike-sharing services: an imbalance between borrowing and returning rates across different stations. This results in some stations having very few bikes, leaving nearby residents unable to borrow one, while other stations become overstocked with bikes that local residents do not need.

To address this issue, APIOBike plans to dispatch a rebalancing truck every night to redistribute bikes across the stations, ensuring that each station maintains an appropriate number of bikes. APIO City has a total of NN stations, numbered 0,1,,N10, 1, \dots, N - 1. Through observation, APIOBike has found that every evening, the number of bikes at station ii is always a fixed value A[i]A[i], and no one borrows or returns bikes at night. The company aims to have exactly B[i]B[i] bikes at station ii every morning.

Among these NN stations, there are N1N - 1 dedicated roads for the rebalancing truck. Each road connects two distinct stations, and the truck is restricted to travelling only on these roads. Every road is one unit long. This network of stations is connected, ensuring that the truck can travel between any pair of stations. Every night, APIOBike must dispatch a rebalancing truck to ensure that each station ii has exactly B[i]B[i] bikes. The truck may start at any station and may end its route at any station.

The truck is empty at the beginning of rebalancing. Whenever the truck is at a station, the driver may load any number of bikes from the station onto the truck, or unload any number of bikes from the truck onto the station. The truck and the stations have unlimited capacities for bikes, but the number of bikes at any location must never drop below zero. The company wants to determine the minimum possible travelling distance to complete the rebalancing and its corresponding strategy.

Implementation Details

You should implement the following procedure.

std::pair<std::vector<int>, std::vector<long long>>
find_rebalancing_strategy(int N,
                          std::vector<int> A,
                          std::vector<int> B,
                          std::vector<int> U,
                          std::vector<int> V)
  • AA: an array of length NN, where A[i]A[i] is the number of bikes at station ii each evening.
  • BB: an array of length NN, where B[i]B[i] is the target number of bikes at station ii each morning.
  • U,VU, V: arrays of length N1N - 1 representing the road network. For each 0i<N10 \le i < N - 1, the ii-th road connects station U[i]U[i] and station V[i]V[i].
  • This procedure is called at most 150 000150\ 000 times for each test case.

The procedure should return a pair of arrays (X,Y)(X, Y) of equal length k+1k + 1, representing a rebalancing strategy:

  • XX: the sequence of station indices visited in chronological order.
  • YY: the net bike delivery at each visited station. For each 0jk0 \le j \le k:
    • If Y[j]0Y[j] \ge 0, the driver unloads Y[j]Y[j] bikes at station X[j]X[j] from the truck, increasing the bike count of the station by Y[j]Y[j].
    • If Y[j]<0Y[j] < 0, the driver loads Y[j]-Y[j] bikes from station X[j]X[j] onto the truck, decreasing the bike count of the station by Y[j]-Y[j].

A valid rebalancing strategy (X,Y)(X, Y) with travelling distance kk must satisfy the following conditions:

  • For each 0jk0 \le j \le k, 0X[j]<N0 \le X[j] < N.
  • For each 0j<k0 \le j < k, stations X[j]X[j] and X[j+1]X[j + 1] are directly connected by a road.
  • For each 0jk0 \le j \le k, t=0jY[t]0\sum_{t=0}^{j} Y[t] \le 0.
  • For each station 0i<N0 \le i < N and each step 0jk0 \le j \le k, let Si,jS_{i,j} be sum of Y[t]Y[t] over all steps tt such that 0tj0 \le t \le j and X[t]=iX[t] = i.
    • The number of bikes at station ii never drops below zero: A[i]+Si,j0A[i] + S_{i,j} \ge 0.
    • After the strategy concludes, each station ii must contain exactly B[i]B[i] bikes: A[i]+Si,k=B[i].A[i] + S_{i,k} = B[i].

输入格式

The first line of the input should contain a single integer TT, the number of scenarios. A description of TT scenarios should follow, each in the format specified below.

Input format:

N
A[0] A[1] ... A[N-1]
B[0] B[1] ... B[N-1]
U[0] V[0]
U[1] V[1]
...
U[N-2] V[N-2]

输出格式

Output format:

k
X[0] X[1] ... X[k]
Y[0] Y[1] ... Y[k]


提示

Example

Example 1

Consider the following call:

find_rebalancing_strategy(4,
                          [10, 1, 5, 0],
                          [10, 0, 3, 3],
                          [0, 1, 1],
                          [1, 2, 3])

:::align{center} :::

There are N=4N = 4 stations in APIO City. Initially, the stations have A=[10,1,5,0]A = [10, 1, 5, 0] bikes, and the goal is to have B=[10,0,3,3]B = [10, 0, 3, 3] bikes at every station.

Suppose the rebalancing truck starts at station 2, and follows these steps:

  • At station 2, load 2 bikes onto the truck. Station 2 now has 3 bikes, and the truck has 2 bikes.
  • Move to station 1 and load 1 bike onto the truck. Station 1 now has 0 bikes, and the truck has 3 bikes.
  • Move to station 3, and unload 3 bikes. Station 3 now has 3 bikes, and the truck has 0 bikes.

The total moving distance is 2. The corresponding return arrays for this strategy are X=[2,1,3]X = [2, 1, 3] and Y=[2,1,3]Y = [-2, -1, 3]. It can be proven that this strategy achieves the minimum distance. Thus, the procedure should return ([2, 1, 3], [-2, -1, 3]).

Example 2

Consider the following call:

find_rebalancing_strategy(5,
                          [3, 0, 1, 2, 2],
                          [2, 2, 1, 3, 0],
                          [2, 2, 2, 2],
                          [0, 4, 3, 1])

:::align{center} :::

There are N=5N = 5 stations in APIO City. Initially, the stations have A=[3,0,1,2,2]A = [3, 0, 1, 2, 2] bikes, and the goal is to have B=[2,2,1,3,0]B = [2, 2, 1, 3, 0] bikes at every station.

Suppose the rebalancing truck starts at station 0, and follows these steps:

  • At station 0, load 1 bike.
  • Move to station 2 and load 1 bike.
  • Move to station 1 and unload 2 bikes.
  • Move to station 2.
  • Move to station 4 and load 2 bikes.
  • Move to station 2 and unload 1 bike.
  • Move to station 3 and unload 1 bike.

The total moving distance is 6. The corresponding return arrays for this strategy are X=[0,2,1,2,4,2,3]X = [0, 2, 1, 2, 4, 2, 3] and Y=[1,1,2,0,2,1,1]Y = [-1, -1, 2, 0, -2, 1, 1]. It can be proven that this strategy achieves the minimum distance. Thus, the procedure should return ([0, 2, 1, 2, 4, 2, 3], [-1, -1, 2, 0, -2, 1, 1]).

Other valid strategies with distance 6, such as X=[0,2,3,2,4,2,1]X = [0, 2, 3, 2, 4, 2, 1] and Y=[1,0,1,0,2,0,2]Y = [-1, 0, 1, 0, -2, 0, 2], are also considered correct. Returning arrays X,YX, Y with length 7=6+17 = 6 + 1 but not a valid strategy, such as X=Y=[0,0,0,0,0,0,0]X = Y = [0, 0, 0, 0, 0, 0, 0], will receive 50%50\% of the score.

Example 3

Consider the following call:

find_rebalancing_strategy(4,
                          [3, 0, 5, 0],
                          [2, 2, 3, 1],
                          [0, 1, 2],
                          [1, 2, 3])

:::align{center} :::

An optimal solution is X=[2,1,0,1,2,3]X = [2, 1, 0, 1, 2, 3] and Y=[1,1,1,1,1,1]Y = [-1, 1, -1, 1, -1, 1]. The procedure should return ([2, 1, 0, 1, 2, 3], [-1, 1, -1, 1, -1, 1]). Other valid strategies, such as X=[2,1,0,1,2,3]X = [2, 1, 0, 1, 2, 3] and Y=[2,2,1,0,0,1]Y = [-2, 2, -1, 0, 0, 1], are also considered correct.

This example satisfies the constraints of subtasks 2 and 3.

Constraints

  • 2N300 0002 \le N \le 300\ 000
  • The sum of NN over all calls to find_rebalancing_strategy does not exceed 300 000300\ 000 in each test case.
  • 0U[i],V[i]<N0 \le U[i], V[i] < N and U[i]V[i]U[i] \neq V[i] for each ii such that 0i<N0 \le i < N.
  • 0A[i],B[i]1090 \le A[i], B[i] \le 10^9 for each ii such that 0i<N0 \le i < N.
  • It is possible to travel between any pair of stations.
  • i=0N1A[i]=i=0N1B[i]\sum_{i=0}^{N-1} A[i] = \sum_{i=0}^{N-1} B[i].
  • There exists at least one ii such that 0i<N0 \le i < N and A[i]B[i]A[i] \neq B[i].

Subtasks and Scoring

Here, TT is the number of calls to find_rebalancing_strategy, and N\sum N is the sum of all NN over all calls to find_rebalancing_strategy.

Subtask Score Additional Constraints
11 44 The stations and roads satisfy property P (see below). There exists exactly one ii such that 0i<N0 \le i < N and A[i]>0A[i] > 0.
22 1111 T10T \le 10. N7N \le 7. The stations and roads satisfy property P.
33 1616 The stations and roads satisfy property P.
44 99 There exists exactly one ii such that 0i<N0 \le i < N and A[i]>0A[i] > 0.
55 2424 N500\sum N \le 500.
66 1515 N5000\sum N \le 5000.
77 2121 No additional constraints.

Property P: For each 0i<N10 \le i < N - 1, U[i]=iU[i] = i and V[i]=i+1V[i] = i + 1. For each 0i<N0 \le i < N, A[i]B[i]A[i] \neq B[i].

If the return arrays XX and YY have length exactly k+1k^* + 1, where kk^* is the minimum possible travelling distance, but (X,Y)(X, Y) is not a valid strategy, you will receive 50%50\% of the score.