#P9601. [IOI 2023] 最长路程
[IOI 2023] 最长路程
Background
IOI 2023 D1T2.
Special reminder: Due to the special nature of Luogu's interactive mechanism, you cannot include header files in your program. Instead, you must paste the contents of the header file at the beginning of your source file. That is, add the following lines at the start of your program:
#include <vector>
std::vector<int> longest_trip(int N, int D);
bool are_connected(std::vector<int> A, std::vector<int> B);
Problem Description
The IOI 2023 organizing committee is in big trouble! They forgot to plan the upcoming trip to Ópusztaszer. However, maybe it is not too late yet......
In Ópusztaszer, there are landmarks, numbered from to . Some pairs of landmarks are connected by two-way roads. Between any pair of landmarks, there is at most one road. The committee does not know which pairs of landmarks are connected by roads.
If, for every three distinct landmarks, there are at least roads among them, we say that the road network density of Ópusztaszer is at least . In other words, for every triple of landmarks with , among the pairs , , and , at least of these pairs are connected by a road.
The committee knows that there exists a positive integer such that the road network density is at least . Note that will not be greater than .
The committee can query the telephone operator in Ópusztaszer to obtain information about road connections between some landmarks. In each query, you must provide two non-empty arrays of landmarks and . All landmarks must be pairwise distinct, i.e.:
- For all and with , we have .
- For all and with , we have .
- For all with and with , we have .
For each query, the operator reports whether there exists a road connecting some landmark in with some landmark in . More precisely, the operator checks all pairs and with and . If there exists a road between and for at least one such pair, the operator reports true. Otherwise, the operator reports false.
A trip of length is defined as a sequence of distinct landmarks such that for all from to (inclusive), there is a road connecting landmarks and . If there is no trip of length at least , then a trip of length is called a longest trip.
Your task is to help the committee find a longest trip in Ópusztaszer by querying the operator.
[Implementation Details]
You need to implement the following function:
int[] longest_trip(int N, int D)
- : the number of landmarks in Ópusztaszer.
- : the guaranteed minimum value of the road network density.
- This function should return an array representing a longest trip .
- For each test case, this function may be called multiple times.
The above function may call the following function:
bool are_connected(int[] A, int[] B)
- : a non-empty array of landmarks with all elements pairwise distinct.
- : a non-empty array of landmarks with all elements pairwise distinct.
- and must be disjoint.
- This function returns
trueif there exists a road connecting some landmark in and some landmark in . Otherwise, it returnsfalse. - In each call to
longest_trip, this function may be called at most times. The total number of calls across all invocations is at most . - If we sum up the lengths of arrays and over all calls, the total combined length must not exceed .
The grader is non-adaptive. Each submission is evaluated on the same set of test cases. In other words, for each test case, the values of and , as well as the pairs of landmarks connected by roads, remain the same for every call to longest_trip.
Hint
[Examples]
Sample 1
Consider a scenario with , , where the roads are connected as shown in the figure below:

The function longest_trip is called as follows:
longest_trip(5, 1)
This function may call are_connected as follows.
| Call | Pairs connected by roads | Return value |
|---|---|---|
are_connected([0], [1, 2, 4, 3]) |
and | true |
are_connected([2], [0]) |
||
are_connected([2], [3]) |
||
are_connected([1, 0], [4, 3]) |
none | false |
After the fourth call, we know that among , , , and , none of these pairs of landmarks is connected by a road. Since the road network density is at least , from the triple we can conclude that the pair must be connected by a road. Similarly, landmarks and must be connected.
At this point, we can conclude that is a trip of length , and there is no trip of length greater than . Therefore, the function longest_trip may return .
Consider another scenario with , , where the roads between landmarks are as shown in the figure below:

The function longest_trip is called as follows:
longest_trip(4, 1)
In this scenario, the longest trip has length . Therefore, after a small number of calls to are_connected, the function longest_trip may return any of , , , and .
Sample 2
Subtask 0 contains another test case used as an example, with landmarks.
[Constraints]
- .
- For each test case, across all calls to
longest_trip, the total sum of does not exceed . - .
[Subtasks]
- (5 points) .
- (10 points) .
- (25 points) . Let denote the length of the longest trip. The function
longest_tripdoes not need to return a trip of length , but should return a trip of length at least . - (60 points) .
In subtask 4, your score depends on the number of calls to are_connected made during a single call to longest_trip. Let be the maximum number of are_connected calls over all calls to longest_trip across all test cases in this subtask.
Your score for this subtask is computed according to the following table:
| Condition | Score |
|---|---|
If, on some test case, the calls to are_connected do not follow the limits given in the implementation details section, or the array returned by longest_trip is incorrect, your score for this subtask will be .
[Sample Grader]
Let be the number of scenarios, i.e. the number of calls to longest_trip.
The sample grader reads input data in the following format:
- Line : .
Then follow the descriptions of these scenarios.
For each scenario, the sample grader reads data in the following format:
- Line : .
- Line (): .
Here, each () is an array of length , used to specify which pairs of landmarks are connected by roads. For all and with and :
- If landmarks and are connected by a road, then should be .
- If landmarks and are not connected by a road, then should be .
In each scenario, before calling longest_trip, the sample grader checks whether the road network density is at least . If this condition is not satisfied, the sample grader outputs Insufficient Density and terminates.
If a rule violation is detected, the sample grader outputs Protocol Violation: <MSG>, where <MSG> is one of the following error messages:
invalid array: in some call toare_connected, at least one of arrays and- is empty, or
- contains an element that is not an integer between and (inclusive), or
- contains duplicate elements.
non-disjoint arrays: in some call toare_connected, the intersection of arrays and is not empty.too many calls: the number of calls toare_connectedin the current call tolongest tripexceeds , or the total number of calls exceeds .too many elements: across all calls toare_connected, the total number of passed landmarks exceeds .
Otherwise, let the array returned by longest_trip in some scenario be , where is a non-negative integer. The sample grader outputs three lines for that scenario in the following format:
- Line : .
- Line : .
- Line : the number of calls to
are_connectedmade in that scenario.
Finally, the sample grader outputs:
- Line : the maximum number of times
are_connectedwas called over all invocations oflongest_trip.
Translated by ChatGPT 5