#P8495. [IOI 2022] 千岛

[IOI 2022] 千岛

Background

Do not #include "islands.h"\texttt{\#include "islands.h"}.

Problem Description

Islands is a beautiful group of islands in the Java Sea. There are NN islands, numbered from 00 to N1N - 1.

There are MM canoes traveling between the islands, numbered from 00 to M1M - 1. For all ii with 0iM10 \le i \le M - 1, canoe ii can dock at island UiU_i or ViV_i, and can travel between islands UiU_i and ViV_i. More specifically, when the canoe is docked at island UiU_i, you can use it to go from island UiU_i to island ViV_i, after which the canoe becomes docked at island ViV_i. Similarly, when the canoe is docked at island ViV_i, it can sail from island ViV_i to island UiU_i, after which the canoe becomes docked at island UiU_i. Initially, the canoe is docked at island UiU_i. 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 ii, you must use some other canoe before you can use canoe ii 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 00 and end at island 00.
  • She must visit at least one island other than island 00.
  • 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 ii with 0iM10 \le i \le M - 1, the canoe must be docked at island UiU_i.

Help Bu Dengklek find a valid journey that uses at most 2  000  0002\;000\;000 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 2  000  0002\;000\;000 trips.

Input Format

You need to implement the following function:

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: two arrays of length MM 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 2  000  0002\;000\;000 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 2  000  0002\;000\;000 integers, or return an integer array that does not describe a valid journey (see the “Subtasks” section for more details).
  • 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 00, 11, 22, and 44 in this order. As a result, she will be on island 11. After that, Bu Dengklek can use canoe 00 again, because the canoe is currently docked at island 11, and the previous canoe she used was not 00. After using canoe 00 again, Bu Dengklek is now on island 00. However, canoes 11, 22, and 44 are not docked at the islands where they were docked before the journey started. Next, Bu Dengklek continues her journey, using canoes 33, 22, 11, 44, and using 33 once again. Bu Dengklek returns to island 00, and all canoes are docked at the same islands where they were docked before the journey started.

Therefore, the return value [0,1,2,4,0,3,2,1,4,3][0, 1, 2, 4, 0, 3, 2, 1, 4, 3] 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 00, and then she can use canoe 11 or 22. Note that she cannot use canoe 00 twice in a row. In both cases, Bu Dengklek returns to island 00. 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 00 is the one she just used. Since no valid journey exists, the function should return false.

Hint

Constraints

  • 2N1052 \le N \le 10^5.
  • 1M2×1051 \le M \le 2 \times 10^5.
  • 0UiN10 \le U_i \le N - 1 and 0ViN10 \le V_i \le N - 1 (for all ii with 0iM10 \le i \le M - 1).
  • UiViU_i \neq V_i (for all ii with 0iM10 \le i \le M - 1).

Subtasks

  1. (5 points) N=2N = 2.
  2. (5 points) N400N \le 400.
    For every pair of distinct islands xx and yy (0x<yN10 \le x \lt y \le N - 1), there are exactly two canoes that can travel between them.
    One of them is initially docked at island xx, and the other is initially docked at island yy.
  3. (21 points) N1000N \le 1000, MM is even, and for all even ii with 0iM10 \le i \le M - 1, canoes ii and i+1i + 1 can both be used to travel between islands UiU_i and ViV_i. Canoe ii is initially docked at island UiU_i, and canoe i+1i + 1 is initially docked at island ViV_i. Formally, Ui=Vi+1U_i = V_{i + 1} and Vi=Ui+1V_i = U_{i + 1}.
  4. (24 points) N1000N \le 1000, MM is even, and for all even ii with 0iM10 \le i \le M - 1, canoes ii and i+1i + 1 can both be used to travel between islands UiU_i and ViV_i. Both canoes are initially docked at island UiU_i.
    Formally, Ui=Ui+1U_i = U_{i + 1} and Vi=Vi+1V_i = V_{i + 1}.
  5. (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 35%35\% of the score if it returns true, or returns an array with more than 2  000  0002\;000\;000 integers, or returns an array that does not describe a valid journey,
  • scores 00 in other cases.

For each testcase where no valid journey exists, your solution:

  • gets full score if it returns false,
  • scores 00 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 11: N  MN \; M.
  • Line 2+i2 + i (0iM10 \le i \le M - 1): Ui  ViU_i \; V_i.

The sample grader prints your output in the following format:

  • If find_journey returns a bool:
    • Line 11: 00.
    • Line 22: 00 if find_journey returns false, otherwise 11.
  • If find_journey returns an int[], and the elements of the array are 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].

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