Singapore's Internet Backbone (SIB) consists of stations, which are assigned indices from to . There are also bidirectional links, numbered from to . Each link connects two distinct stations. Two stations connected with a single link are called neighbours.
A path from station to station is a sequence of distinct stations such that , , and every two consecutive stations in the path are neighbours. There is exactly one path from any station to any station .
Any station can create a packet (a piece of data) and send it to any other station , which is called the packet's target. This packet must be routed along the unique path from to as follows. Consider a station that currently holds a packet, whose target station is . In this situation station :
- executes a routing procedure that determines the neighbour of which is on the unique path from to , and
- forwards the packet to this neighbour.
However, stations have limited memory and do not share the entire list of the links in SIB to use it in the routing procedure.
Your task is to implement a routing scheme in SIB, which consists of two procedures.
- The first procedure is given , the list of the links in SIB and an integer as the inputs. It assigns each station a unique integer label between and , inclusive.
The second procedure is the routing procedure, which is deployed to all stations after labels are assigned. It is given only the following inputs:
- , the label of the station that currently holds the packet,
- , the label of the packet's target station ,
- , the list of the labels of all neighbours of .
It should return the label of the neighbour of that the packet should be forwarded to.
In one subtask, the score of your solution depends on the value of the maximum label assigned to any station (in general, smaller is better).
Implementation Details
You should implement the following procedures:
std::vector<int> label(int n, int k, std::vector<int> u, std::vector<int> v)
- : number of stations in SIB.
- : maximum label that can be used.
- and : array of sizes describing the links. For each , link connects stations with indices and .
- This procedure should return a single array of size . For each , is the label assigned to the station with index . All elements of array must be unique and between and , inclusive.
int find_next_station(int s, int t, std::vector<int> c)
- : label of the station holding the packet.
- : label of the packet's target station.
- : an array giving the list of the labels of all neighbours of . The array is sorted in ascending order.
- This procedure should return the label of a neighbour of that the packet should be forwarded to.
Each test case involves one or more independent scenarios (i.e., different SIB descriptions). For a test case involving scenarios, a program that calls the above procedures is run exactly two times, as follows.
During the first run of the program:
label
procedure is called times,- the returned labels are stored by the grading system, and
find_next_station
is not called.
During the second run of the program:
find_next_station
may be called multiple times. In each call, an arbitrary scenario is chosen, and the labels returned by the call tolabel
procedure in that scenario are used as the inputs tofind_next_station
.label
is not called.
In particular, any information saved to static or global variables in the first run of the program is not available within the find_next_station
procedure.
Example
Consider the following call:
label(5, 10, [0, 1, 1, 2], [1, 2, 3, 4])
There are a total of stations, and links connecting pairs of stations with indices , , , and . Each label can be an integer from to .
In order to report the following labeling:
Index | Label |
---|---|
The label
procedure should return . The numbers in the following figure show the indices (left panel) and assigned labels (right panel).
Assume the labels have been assigned as described above and consider the following call:
find_next_station(9, 6, [2, 7])
This means that the station holding the packet has label , and the target station has label . The labels of stations on the path to the target station are . Hence, the call should return , which is the label of the station that the packet should be forwarded to (which has index ).
Consider another possible call:
find_next_station(2, 3, [3, 6, 9])
The procedure should return , since the target station with label is a neighbour of the station with label , and hence should receive the packet directly.
Constraints
For each call to label
:
- (for all )
For each call to find_next_station
, the input comes from an arbitrarily chosen previous call to label
. Consider the labels it produced. Then:
- and are labels of two different stations.
- is the sequence of all labels of neighbours of the station with label , in ascending order.
For each test case, the total length of all arrays passed to the procedure find_next_station
does not exceed for all scenarios combined.
Subtasks
- ( points) , no station has more than neighbours.
- ( points) , link connects station and .
- ( points) , at most one station has more than neighbours.
- ( points) , .
- ( points) .
In subtask , you can obtain a partial score. Let be the maximum label value returned by label
across all scenarios.
Your score for this subtask is calculated according to the following table:
Maximum label | Score |
---|---|
Sample Grader
The sample grader reads the input in the following format:
- line :
blocks follow, each describing a single scenario. The format of each block is as follows:
- line :
- line :
- line : : the number of calls to
find_next_station
. - line : : indices of stations involved in the -th call to
find_next_station
. The station holds the packet, the station is the packet's target, and the station is the station that the packet should be forwarded to.
The sample grader prints the result in the following format:
- line :
blocks corresponding to the consecutive scenarios in the input follow. The format of each block is as follows:
- line : index of the station, whose label was returned by the -th call to
find_next_station
in this scenario.
Note that each run of the sample grader calls both label
and find_next_station
.
Attachment Package
The sample grader along with sample test cases are available here.
Comments