#P8495. [IOI 2022] 千岛
[IOI 2022] 千岛
Background
Do not .
Problem Description
Islands is a beautiful group of islands in the Java Sea. There are islands, numbered from to .
There are canoes traveling between the islands, numbered from to . For all with , canoe can dock at island or , and can travel between islands and . More specifically, when the canoe is docked at island , you can use it to go from island to island , after which the canoe becomes docked at island . Similarly, when the canoe is docked at island , it can sail from island to island , after which the canoe becomes docked at island . Initially, the canoe is docked at island . There may be multiple canoes that can be used to travel between the same pair of islands. There may also be multiple canoes docked at the same island.
For safety reasons, each canoe must be repaired after each trip, so the same canoe is not allowed to be used for two consecutive trips. That is, after using some canoe , you must use some other canoe before you can use canoe again.
Bu Dengklek wants to plan a journey among some islands. Her journey is valid if and only if it satisfies the following conditions:
- Her journey must start at island and end at island .
- She must visit at least one island other than island .
- After the journey ends, every canoe must be docked at the same island where it was docked before the journey started. That is, for all with , the canoe must be docked at island .
Help Bu Dengklek find a valid journey that uses at most trips, or tell her that no valid journey exists.
It can be proven that under the constraints of this task (see the Constraints section), if a valid journey exists, then there also exists a valid journey with at most trips.
Input Format
You need to implement the following function:
union(bool, int[]) find_journey(int N, int M, int[] U, int[] V)
- : the number of islands.
- : the number of canoes.
- , : two arrays of length that describe the islands connected by each canoe.
- The function should return either a boolean or an integer array.
- If no valid journey exists, the function should return
false. - If a valid journey exists, you have two choices:
- If you want to get full score, the function should return an array of at most integers that describes a valid journey. More precisely, the elements of the array should be the indices of the canoes used in the journey (in the order they are used).
- If you want to get partial score, the function should return
true, or return an array with more than integers, or return an integer array that does not describe a valid journey (see the “Subtasks” section for more details).
- If no valid journey exists, the function should return
- The function will be called exactly once.
Output Format
Example 1
Consider the following call:
find_journey(4, 5, [0, 1, 2, 0, 3], [1, 2, 3, 3, 1])
The figure below shows the islands and canoes.

One possible valid journey is as follows. Bu Dengklek first uses canoes , , , and in this order. As a result, she will be on island . After that, Bu Dengklek can use canoe again, because the canoe is currently docked at island , and the previous canoe she used was not . After using canoe again, Bu Dengklek is now on island . However, canoes , , and are not docked at the islands where they were docked before the journey started. Next, Bu Dengklek continues her journey, using canoes , , , , and using once again. Bu Dengklek returns to island , and all canoes are docked at the same islands where they were docked before the journey started.
Therefore, the return value describes a valid journey.
Example 2
Consider the following call:
find_journey(2, 3, [0, 1, 1], [1, 0, 0])
The figure below shows the islands and canoes.

Bu Dengklek can only start by using canoe , and then she can use canoe or . Note that she cannot use canoe twice in a row. In both cases, Bu Dengklek returns to island . However, there are canoes that are not docked at the islands where they were docked before the journey started, and after that Bu Dengklek cannot use any canoe anymore, because the only canoe docked at island is the one she just used. Since no valid journey exists, the function should return false.
Hint
Constraints
- .
- .
- and (for all with ).
- (for all with ).
Subtasks
- (5 points) .
- (5 points) .
For every pair of distinct islands and (), there are exactly two canoes that can travel between them.
One of them is initially docked at island , and the other is initially docked at island . - (21 points) , is even, and for all even with , canoes and can both be used to travel between islands and . Canoe is initially docked at island , and canoe is initially docked at island . Formally, and .
- (24 points) , is even, and for all even with , canoes and can both be used to travel between islands and . Both canoes are initially docked at island .
Formally, and . - (45 points) No additional constraints.
For each testcase where a valid journey exists, your solution:
- gets full score if it returns a valid journey,
- gets of the score if it returns
true, or returns an array with more than integers, or returns an array that does not describe a valid journey, - scores in other cases.
For each testcase where no valid journey exists, your solution:
- gets full score if it returns
false, - scores in other cases.
Note that the final score for each subtask is the lowest score among all testcases in that subtask.
Sample grader
The sample grader reads input in the following format:
- Line : .
- Line (): .
The sample grader prints your output in the following format:
- If
find_journeyreturns abool:- Line : .
- Line : if
find_journeyreturnsfalse, otherwise .
- If
find_journeyreturns anint[], and the elements of the array are , the sample grader prints:- Line : .
- Line : .
- Line : .
Notes
In the statement, when the function interface is given, the generic type names void, bool, int, int[] (array), and union(bool, int[]) are used.
In C++, the grader will use appropriate data types or implementations, as shown in the table below:
void |
bool |
int |
int[] |
|---|---|---|---|
void |
bool |
int |
std::vector<int> |
union(bool, int[]) |
Length of array a |
|---|---|
std::variant<bool, std::vector<int>> |
a.size() |
In C++, std::variant is defined in the header file <variant>.
A function with return type std::variant<bool, std::vector<int>> can return either a bool or a std::vector<int>.
The following sample code shows three functions that return std::variant, and all of them work correctly:
std::variant<bool, std::vector<int>> foo(int N) {
return N % 2 == 0;
}
std::variant<bool, std::vector<int>> goo(int N) {
return std::vector<int>(N, 0);
}
std::variant<bool, std::vector<int>> hoo(int N) {
if (N % 2 == 0) {
return false;
}
return std::vector<int>(N, 0);
}
Translated by ChatGPT 5