## IOI '22 P6 - Thousands Islands

View as PDF

Points: 30 (partial)
Time limit: 2.0s
Memory limit: 512M

Problem types
Allowed languages
C++

Thousands Islands is a group of beautiful islands located in the Java Sea. It consists of islands, numbered from to .

There are canoes, numbered from to , that can be used to sail between islands. For each such that , canoe can be docked either at island or , and can be used to sail between islands and . Specifically, when the canoe is docked at island , it can be used to sail from island to island , after which the canoe becomes docked at island . Similarly, when the canoe is docked at island , it can be used to sail from island to island , after which the canoe becomes docked at island . Initially, the canoe is docked at island . It is possible that multiple canoes can be used to sail between the same pair of islands. It is also possible that multiple canoes are docked at the same island.

For safety reasons, a canoe needs to be maintained after every time it is sailed, which forbids the same canoe to be sailed two times in a row. That is, after using some canoe , another canoe must be used before canoe can be used again.

Bu Dengklek wants to plan a journey through some of the islands. Her journey is valid if and only if the following conditions are satisfied.

• She starts and ends her journey at island .
• She visits at least one island other than island .
• After the journey ends, each canoe is docked at the same island as it was before the journey. I.e., canoe , for each such that , must be docked at island .

Help Bu Dengklek find any valid journey involving sailing at most times, or determine that no such valid journey exists. It can be proven that under the constraints specified in this task (see Constraints section), if a valid journey exists, there also exists a valid journey that does not involve sailing more than times.

#### Implementation Details

You should implement the following procedure:

std::variant<bool, std::vector<int>> find_journey(int N, int M, std::vector<int> U, std::vector<int> V)

• : the number of islands.
• : the number of canoes.
• , : arrays of length describing the canoes.
• This procedure should return either a boolean or an array of integers.
• If no valid journey exists, the procedure should return false.
• If a valid journey exists, you have two options:
• To be awarded the full score, the procedure should return an array of at most integers representing a valid journey. More precisely, the elements of this array should be the numbers of the canoes that are used in the journey (in the order they are used).
• To be awarded a partial score, the procedure should return true, an array of more than integers, or an array of integers not describing a valid journey. (See the Subtasks section for more details.)
• This procedure is called exactly once.

#### Examples

##### Example 1

Consider the following call:

find_journey(4, 5, {0, 1, 2, 0, 3}, {1, 2, 3, 3, 1})


The islands and canoes are shown in the picture below.

One possible valid journey is as follows. Bu Dengklek first sails canoes , , , and in that order. As a result, she is at island . After that, Bu Dengklek can sail canoe again as it is currently docked at island and the last canoe she used is not canoe . After sailing canoe again, Bu Dengklek is now at island . However, canoes , and are not docked at the same islands as they were before the journey. Bu Dengklek then continues her journey by sailing canoes , , , , and again. Bu Dengklek is back at island and all the canoes are docked at the same islands as before the journey.

Therefore, the returned value represents a valid journey.

##### Example 2

Consider the following call:

find_journey(2, 3, {0, 1, 1}, {1, 0, 0})


The islands and canoes are shown in the picture below.

Bu Dengklek can only start by sailing canoe , after which she can sail either canoe or . Note that she cannot sail canoe twice in a row. In both cases, Bu Dengklek is back at island . However, the canoes are not docked at the same islands as they were before the journey, and Bu Dengklek cannot sail any canoe afterwards, as the only canoe docked at island is the one she has just used. As there is no valid journey, the procedure should return false.

#### Constraints

• and (for each such that )
• (for each such that )

1. (5 points)
2. (5 points) . For each pair of distinct islands and , there are exactly two canoes that can be used to sail between them. One of them is docked at island , and the other one is docked at island .
3. (21 points) , is even, and for each even such that , canoes and can both be used to sail between islands and . Canoe is initially docked at island and canoe is initially docked at island . Formally, and .
4. (24 points) , is even, and for each even such that , canoes and can both be used to sail between islands and . Both canoes are initially docked at island . Formally, and .
5. (45 points) No additional constraints.

For each test case in which a valid journey exists, your solution:

• gets full points if it returns a valid journey,
• gets of the points if it returns true, an array of more than integers, or an array that does not describe a valid journey,
• gets points otherwise.

For each test case in which a valid journey does not exist, your solution:

• gets full points if it returns false,
• gets points otherwise.

Note that the final score for each subtask is the minimum of the points for the test cases in the subtask.

• line :
• line :

• If find_journey returns a bool:
• line :
• line : if find_journey returns false, or otherwise.
• If find_journey returns a std::vector<int>, denote the elements of this array by . The sample grader prints:
• line :
• line :
• line :

#### Attachment Package

The sample grader along with sample test cases are available here.