#IOI20222C. 千岛

千岛

Thousands Islands

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

There are MM canoes, numbered from 00 to M1M - 1, that can be used to sail between islands. For each ii such that 0iM10 \le i \le M - 1, canoe ii can be docked either at island U[i]U[i] or V[i]V[i], and can be used to sail between islands U[i]U[i] and V[i]V[i]. Specifically, when the canoe is docked at island U[i]U[i], it can be used to sail from island U[i]U[i] to island V[i]V[i], after which the canoe becomes docked at island V[i]V[i]. Similarly, when the canoe is docked at island V[i]V[i], it can be used to sail from island V[i]V[i] to island U[i]U[i], after which the canoe becomes docked at island U[i]U[i]. Initially, the canoe is docked at island U[i]U[i]. 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 ii, another canoe must be used before canoe ii 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 00.
  • She visits at least one island other than island 00.
  • After the journey ends, each canoe is docked at the same island as it was before the journey. I.e., canoe ii, for each ii such that 0iM10 \le i \le M - 1, must be docked at island U[i]U[i].

Help Bu Dengklek find any valid journey involving sailing at most 2  000  0002\;000\;000 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 2  000  0002\;000\;000 times.

Implementation Details

You should implement the following procedure:

union(bool, int[]) find_journey(int N, int M, int[] U, int[] V)
  • NN: the number of islands.
  • MM: the number of canoes.
  • UU, VV: arrays of length MM 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 2  000  0002\;000\;000 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 2  000  0002\;000\;000 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 00, 11, 22, and 44 in that order. As a result, she is at island 11. After that, Bu Dengklek can sail canoe 00 again as it is currently docked at island 11 and the last canoe she used is not canoe 00. After sailing canoe 00 again, Bu Dengklek is now at island 00. However, canoes 11, 22 and 44 are not docked at the same islands as they were before the journey. Bu Dengklek then continues her journey by sailing canoes 33, 22, 11, 44, and 33 again. Bu Dengklek is back at island 00 and all the canoes are docked at the same islands as before the journey.

Therefore, the returned value [0,1,2,4,0,3,2,1,4,3][0, 1, 2, 4, 0, 3, 2, 1, 4, 3] 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 00, after which she can sail either canoe 11 or 22. Note that she cannot sail canoe 00 twice in a row. In both cases, Bu Dengklek is back at island 00. 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 00 is the one she has just used. As there is no valid journey, the procedure should return false.

Constraints

  • 2N100  0002 \le N \le 100\;000
  • 1M200  0001 \le M \le 200\;000
  • 0U[i]N10 \le U[i] \le N - 1 and 0V[i]N10 \le V[i] \le N - 1 (for each ii such that 0iM10 \le i \le M - 1)
  • U[i]V[i]U[i] \neq V[i] (for each ii such that 0iM10 \le i \le M - 1)

Subtasks

  1. (5 points) N=2N = 2
  2. (5 points) N400N \le 400. For each pair of distinct islands xx and yy (0x<yN10 \le x \lt y \le N - 1), there are exactly two canoes that can be used to sail between them. One of them is docked at island xx, and the other one is docked at island yy.
  3. (21 points) N1000N \le 1000, MM is even, and for each even ii such that 0iM10 \le i \le M - 1, canoes ii and i+1i + 1 can both be used to sail between islands U[i]U[i] and V[i]V[i]. Canoe ii is initially docked at island U[i]U[i] and canoe i+1i + 1 is initially docked at island V[i]V[i]. Formally, U[i]=V[i+1]U[i] = V[i + 1] and V[i]=U[i+1]V[i] = U[i + 1].
  4. (24 points) N1000N \le 1000, MM is even, and for each even ii such that 0iM10 \le i \le M - 1, canoes ii and i+1i + 1 can both be used to sail between islands U[i]U[i] and V[i]V[i]. Both canoes are initially docked at island U[i]U[i]. Formally, U[i]=U[i+1]U[i] = U[i + 1] and V[i]=V[i+1]V[i] = V[i + 1].
  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 35%35\% of the points if it returns true, an array of more than 2  000  0002\;000\;000 integers, or an array that does not describe a valid journey,
  • gets 00 points otherwise.

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

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

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

Sample Grader

The sample grader reads the input in the following format:

  • line 11: N  MN \; M
  • line 2+i2 + i (0iM10 \le i \le M - 1): U[i]  V[i]U[i] \; V[i]

The sample grader prints your answers in the following format:

  • If find_journey returns a bool:
    • line 11: 00
    • line 22: 00 if find_journey returns false, or 11 otherwise.
  • If find_journey returns an int[], denote the elements of this array by c[0],c[1],c[k1]c[0], c[1], \ldots c[k-1]. The sample grader prints:
    • line 11: 11
    • line 22: kk
    • line 33: c[0]  c[1]    c[k1]c[0] \; c[1] \; \ldots \; c[k-1]