A12344 | Circular Dance
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
There are $n$ kids, numbered from $1$ to $n$ , dancing in a circle around the Christmas tree. Let's enumerate them in a clockwise direction as $p_1$ , $p_2$ , ..., $p_n$ (all these numbers are from $1$ to $n$ and are distinct, so $p$ is a permutation). Let the next kid for a kid $p_i$ be kid $p_{i + 1}$ if $i < n$ and $p_1$ otherwise. After the dance, each kid remembered two kids: the next kid (let's call him $x$ ) and the next kid for $x$ . Each kid told you which kids he/she remembered: the kid $i$ remembered kids $a_{i, 1}$ and $a_{i, 2}$ . However, the order of $a_{i, 1}$ and $a_{i, 2}$ can differ from their order in the circle.
Example: 5 kids in a circle, $p=[3, 2, 4, 1, 5]$ (or any cyclic shift). The information kids remembered is: $a_{1,1}=3$ , $a_{1,2}=5$ ; $a_{2,1}=1$ , $a_{2,2}=4$ ; $a_{3,1}=2$ , $a_{3,2}=4$ ; $a_{4,1}=1$ , $a_{4,2}=5$ ; $a_{5,1}=2$ , $a_{5,2}=3$ .You have to restore the order of the kids in the circle using this information. If there are several answers, you may print any. It is guaranteed that at least one solution exists.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Example: 5 kids in a circle, $p=[3, 2, 4, 1, 5]$ (or any cyclic shift). The information kids remembered is: $a_{1,1}=3$ , $a_{1,2}=5$ ; $a_{2,1}=1$ , $a_{2,2}=4$ ; $a_{3,1}=2$ , $a_{3,2}=4$ ; $a_{4,1}=1$ , $a_{4,2}=5$ ; $a_{5,1}=2$ , $a_{5,2}=3$ .You have to restore the order of the kids in the circle using this information. If there are several answers, you may print any. It is guaranteed that at least one solution exists.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
输入格式
The first line of the input contains one integer $n$ ( $3 \le n \le 2 \cdot 10^5$ ) — the number of the kids.
The next $n$ lines contain $2$ integers each. The $i$ -th line contains two integers $a_{i, 1}$ and $a_{i, 2}$ ( $1 \le a_{i, 1}, a_{i, 2} \le n, a_{i, 1} \ne a_{i, 2}$ ) — the kids the $i$ -th kid remembered, given in arbitrary order.
The next $n$ lines contain $2$ integers each. The $i$ -th line contains two integers $a_{i, 1}$ and $a_{i, 2}$ ( $1 \le a_{i, 1}, a_{i, 2} \le n, a_{i, 1} \ne a_{i, 2}$ ) — the kids the $i$ -th kid remembered, given in arbitrary order.
输出格式
Print $n$ integers $p_1$ , $p_2$ , ..., $p_n$ — permutation of integers from $1$ to $n$ , which corresponds to the order of kids in the circle. If there are several answers, you may print any (for example, it doesn't matter which kid is the first in the circle). It is guaranteed that at least one solution exists.
输入输出样例
输入 #1
5 3 5 1 4 2 4 1 5 2 3
输出 #1
3 2 4 1 5
输入 #2
3 2 3 3 1 1 2
输出 #2
3 1 2
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted