CF925D.Aztec Catacombs

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Indiana Jones found ancient Aztec catacombs containing a golden idol. The catacombs consists of nn caves. Each pair of caves is connected with a two-way corridor that can be opened or closed. The entrance to the catacombs is in the cave 11, the idol and the exit are in the cave nn.

When Indiana goes from a cave xx to a cave yy using an open corridor, all corridors connected to the cave xx change their state: all open corridors become closed, all closed corridors become open. Indiana wants to go from cave 11 to cave nn going through as small number of corridors as possible. Help him find the optimal path, or determine that it is impossible to get out of catacombs.

印第安纳·琼斯在古老的阿兹特克地下墓穴中发现了一尊黄金神像。该墓穴由 nn 个洞穴组成。每对洞穴之间都有一条双向走廊,每条走廊均可开启或关闭。墓穴入口位于洞穴 11,而神像与出口则位于洞穴 nn。

当印第安纳经一条开启的走廊从洞穴 xx 前往洞穴 yy 时,所有与洞穴 xx 相连的走廊状态都会翻转:所有开启的走廊变为关闭,所有关闭的走廊变为开启。印第安纳希望从洞穴 11 出发,以经过尽可能少的走廊为代价抵达洞穴 nn。请帮助他找出最优路径,或判定无法离开该墓穴。

输入格式

The first line contains two integers nn and mm (2≤n≤3⋅1052 \leq n \leq 3\cdot 10^5, 0≤m≤3⋅1050 \leq m \leq 3 \cdot 10^5) — the number of caves and the number of open corridors at the initial moment.

The next mm lines describe the open corridors. The ii-th of these lines contains two integers uiu_i and viv_i (1≤ui,vi≤n1 \leq u_i, v_i \leq n, ui≠viu_i \neq v_i) — the caves connected by the ii-th open corridor. It is guaranteed that each unordered pair of caves is presented at most once.

第一行包含两个整数 nn 和 mm(2≤n≤3⋅1052 \leq n \leq 3\cdot 10^5,0≤m≤3⋅1050 \leq m \leq 3 \cdot 10^5),分别表示洞穴的数量以及初始时刻开放的通道数量。

接下来的 mm 行描述了开放的通道。其中第 ii 行包含两个整数 uiu_i 和 viv_i(1≤ui,vi≤n1 \leq u_i, v_i \leq n,ui≠viu_i \neq v_i),表示第 ii 条开放通道所连接的两个洞穴。保证每对无序洞穴至多出现一次。

输出格式

If there is a path to exit, in the first line print a single integer kk — the minimum number of corridors Indians should pass through (1≤k≤1061 \leq k \leq 10^6). In the second line print k+1k+1 integers x0,…,xkx_0, \ldots, x_k — the number of caves in the order Indiana should visit them. The sequence x0,…,xkx_0, \ldots, x_k should satisfy the following:

  • x0=1x_0 = 1, xk=nx_k = n;
  • for each ii from 11 to kk the corridor from xi−1x_{i - 1} to xix_i should be open at the moment Indiana walks along this corridor.

If there is no path, print a single integer −1-1.

We can show that if there is a path, there is a path consisting of no more than 10610^6 corridors.

如果存在一条通往出口的路径,则在第一行输出一个整数 kk —— 印第安纳需要经过的走廊的最少数量(1≤k≤1061 \leq k \leq 10^6)。在第二行输出 k+1k+1 个整数 x0,…,xkx_0, \ldots, x_k —— 印第安纳应依次访问的洞穴编号。序列 x0,…,xkx_0, \ldots, x_k 应满足以下条件:

  • x0=1x_0 = 1,xk=nx_k = n;
  • 对每个 ii(从 11 到 kk),当印第安纳沿走廊从 xi−1x_{i - 1} 走向 xix_i 时,该走廊必须处于开启状态。

若不存在路径,则输出单个整数 −1-1。

可以证明:若存在路径,则必存在一条由不超过 10610^6 条走廊组成的路径。

输入输出样例

  • 输入#1

    4 4
    1 2
    2 3
    1 3
    3 4

    输出#1

    2
    1 3 4
  • 输入#2

    4 2
    1 2
    2 3

    输出#2

    4
    1 2 3 1 4

输入解题思路,AI测评打分。不知道怎么写?

首页