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 stations, numbered . Through observation, APIOBike has found that every evening, the number of bikes at station is always a fixed value , and no one borrows or returns bikes at night. The company aims to have exactly bikes at station every morning.
Among these stations, there are 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 has exactly 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.
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)
The procedure should return a pair of arrays of equal length , representing a rebalancing strategy:
A valid rebalancing strategy with travelling distance must satisfy the following conditions:
The first line of the input should contain a single integer , the number of scenarios. A description of 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]
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 stations in APIO City. Initially, the stations have bikes, and the goal is to have bikes at every station.
Suppose the rebalancing truck starts at station 2, and follows these steps:
The total moving distance is 2. The corresponding return arrays for this strategy are and . It can be proven that this strategy achieves the minimum distance. Thus, the procedure should return ([2, 1, 3], [-2, -1, 3]).
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 stations in APIO City. Initially, the stations have bikes, and the goal is to have bikes at every station.
Suppose the rebalancing truck starts at station 0, and follows these steps:
The total moving distance is 6. The corresponding return arrays for this strategy are and . 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 and , are also considered correct. Returning arrays with length but not a valid strategy, such as , will receive of the score.
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 and . The procedure should return ([2, 1, 0, 1, 2, 3], [-1, 1, -1, 1, -1, 1]). Other valid strategies, such as and , are also considered correct.
This example satisfies the constraints of subtasks 2 and 3.
find_rebalancing_strategy does not exceed in each test case.Here, is the number of calls to find_rebalancing_strategy, and is the sum of all over all calls to find_rebalancing_strategy.
| Subtask | Score | Additional Constraints |
|---|---|---|
| The stations and roads satisfy property P (see below). There exists exactly one such that and . | ||
| . . The stations and roads satisfy property P. | ||
| The stations and roads satisfy property P. | ||
| There exists exactly one such that and . | ||
| . | ||
| . | ||
| No additional constraints. |
Property P: For each , and . For each , .
If the return arrays and have length exactly , where is the minimum possible travelling distance, but is not a valid strategy, you will receive of the score.