A11653 | Aztec Catacombs
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Indiana Jones found ancient Aztec catacombs containing a golden idol. The catacombs consists of $n$ 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 $1$ , the idol and the exit are in the cave $n$ .
When Indiana goes from a cave $x$ to a cave $y$ using an open corridor, all corridors connected to the cave $x$ change their state: all open corridors become closed, all closed corridors become open. Indiana wants to go from cave $1$ to cave $n$ 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.
When Indiana goes from a cave $x$ to a cave $y$ using an open corridor, all corridors connected to the cave $x$ change their state: all open corridors become closed, all closed corridors become open. Indiana wants to go from cave $1$ to cave $n$ 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.
输入格式
The first line contains two integers $n$ and $m$ ( $2 \leq n \leq 3\cdot 10^5$ , $0 \leq m \leq 3 \cdot 10^5$ ) — the number of caves and the number of open corridors at the initial moment.
The next $m$ lines describe the open corridors. The $i$ -th of these lines contains two integers $u_i$ and $v_i$ ( $1 \leq u_i, v_i \leq n$ , $u_i \neq v_i$ ) — the caves connected by the $i$ -th open corridor. It is guaranteed that each unordered pair of caves is presented at most once.
The next $m$ lines describe the open corridors. The $i$ -th of these lines contains two integers $u_i$ and $v_i$ ( $1 \leq u_i, v_i \leq n$ , $u_i \neq v_i$ ) — the caves connected by the $i$ -th open corridor. It is guaranteed that each unordered pair of caves is presented at most once.
输出格式
If there is a path to exit, in the first line print a single integer $k$ — the minimum number of corridors Indians should pass through ( $1 \leq k \leq 10^6$ ). In the second line print $k+1$ integers $x_0, \ldots, x_k$ — the number of caves in the order Indiana should visit them. The sequence $x_0, \ldots, x_k$ should satisfy the following:
- $x_0 = 1$ , $x_k = n$ ;
- for each $i$ from $1$ to $k$ the corridor from $x_{i - 1}$ to $x_i$ should be open at the moment Indiana walks along this corridor.
If there is no path, print a single integer $-1$ .
We can show that if there is a path, there is a path consisting of no more than $10^6$ corridors.
- $x_0 = 1$ , $x_k = n$ ;
- for each $i$ from $1$ to $k$ the corridor from $x_{i - 1}$ to $x_i$ should be open at the moment Indiana walks along this corridor.
If there is no path, print a single integer $-1$ .
We can show that if there is a path, there is a path consisting of no more than $10^6$ corridors.
输入输出样例
输入 #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
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted