A15132 | Unordered Swaps
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Alice had a permutation $p$ of numbers from $1$ to $n$ . Alice can swap a pair $(x, y)$ which means swapping elements at positions $x$ and $y$ in $p$ (i.e. swap $p_x$ and $p_y$ ). Alice recently learned her first sorting algorithm, so she decided to sort her permutation in the minimum number of swaps possible. She wrote down all the swaps in the order in which she performed them to sort the permutation on a piece of paper.
For example,
- $[(2, 3), (1, 3)]$ is a valid swap sequence by Alice for permutation $p = [3, 1, 2]$ whereas $[(1, 3), (2, 3)]$ is not because it doesn't sort the permutation. Note that we cannot sort the permutation in less than $2$ swaps.
- $[(1, 2), (2, 3), (2, 4), (2, 3)]$ cannot be a sequence of swaps by Alice for $p = [2, 1, 4, 3]$ even if it sorts the permutation because $p$ can be sorted in $2$ swaps, for example using the sequence $[(4, 3), (1, 2)]$ .
Unfortunately, Bob shuffled the sequence of swaps written by Alice.
You are given Alice's permutation $p$ and the swaps performed by Alice in arbitrary order. Can you restore the correct sequence of swaps that sorts the permutation $p$ ? Since Alice wrote correct swaps before Bob shuffled them up, it is guaranteed that there exists some order of swaps that sorts the permutation.
For example,
- $[(2, 3), (1, 3)]$ is a valid swap sequence by Alice for permutation $p = [3, 1, 2]$ whereas $[(1, 3), (2, 3)]$ is not because it doesn't sort the permutation. Note that we cannot sort the permutation in less than $2$ swaps.
- $[(1, 2), (2, 3), (2, 4), (2, 3)]$ cannot be a sequence of swaps by Alice for $p = [2, 1, 4, 3]$ even if it sorts the permutation because $p$ can be sorted in $2$ swaps, for example using the sequence $[(4, 3), (1, 2)]$ .
Unfortunately, Bob shuffled the sequence of swaps written by Alice.
You are given Alice's permutation $p$ and the swaps performed by Alice in arbitrary order. Can you restore the correct sequence of swaps that sorts the permutation $p$ ? Since Alice wrote correct swaps before Bob shuffled them up, it is guaranteed that there exists some order of swaps that sorts the permutation.
输入格式
The first line contains $2$ integers $n$ and $m$ $(2 \le n \le 2 \cdot 10^5, 1 \le m \le n - 1)$ — the size of permutation and the minimum number of swaps required to sort the permutation.
The next line contains $n$ integers $p_1, p_2, ..., p_n$ ( $1 \le p_i \le n$ , all $p_i$ are distinct) — the elements of $p$ . It is guaranteed that $p$ forms a permutation.
Then $m$ lines follow. The $i$ -th of the next $m$ lines contains two integers $x_i$ and $y_i$ — the $i$ -th swap $(x_i, y_i)$ .
It is guaranteed that it is possible to sort $p$ with these $m$ swaps and that there is no way to sort $p$ with less than $m$ swaps.
The next line contains $n$ integers $p_1, p_2, ..., p_n$ ( $1 \le p_i \le n$ , all $p_i$ are distinct) — the elements of $p$ . It is guaranteed that $p$ forms a permutation.
Then $m$ lines follow. The $i$ -th of the next $m$ lines contains two integers $x_i$ and $y_i$ — the $i$ -th swap $(x_i, y_i)$ .
It is guaranteed that it is possible to sort $p$ with these $m$ swaps and that there is no way to sort $p$ with less than $m$ swaps.
输出格式
Print a permutation of $m$ integers — a valid order of swaps written by Alice that sorts the permutation $p$ . See sample explanation for better understanding.
In case of multiple possible answers, output any.
In case of multiple possible answers, output any.
输入输出样例
输入 #1
4 3 2 3 4 1 1 4 2 1 1 3
输出 #1
2 3 1
输入 #2
6 4 6 5 1 3 2 4 3 1 2 5 6 3 6 4
输出 #2
4 1 3 2
In the first example, $p = [2, 3, 4, 1]$ , $m = 3$ and given swaps are $[(1, 4), (2, 1), (1, 3)]$ .
There is only one correct order of swaps i.e $[2, 3, 1]$ .
1. First we perform the swap $2$ from the input i.e $(2, 1)$ , $p$ becomes $[3, 2, 4, 1]$ .
2. Then we perform the swap $3$ from the input i.e $(1, 3)$ , $p$ becomes $[4, 2, 3, 1]$ .
3. Finally we perform the swap $1$ from the input i.e $(1, 4)$ and $p$ becomes $[1, 2, 3, 4]$ which is sorted.
In the second example, $p = [6, 5, 1, 3, 2, 4]$ , $m = 4$ and the given swaps are $[(3, 1), (2, 5), (6, 3), (6, 4)]$ .
One possible correct order of swaps is $[4, 2, 1, 3]$ .
1. Perform the swap $4$ from the input i.e $(6, 4)$ , $p$ becomes $[6, 5, 1, 4, 2, 3]$ .
2. Perform the swap $2$ from the input i.e $(2, 5)$ , $p$ becomes $[6, 2, 1, 4, 5, 3]$ .
3. Perform the swap $1$ from the input i.e $(3, 1)$ , $p$ becomes $[1, 2, 6, 4, 5, 3]$ .
4. Perform the swap $3$ from the input i.e $(6, 3)$ and $p$ becomes $[1, 2, 3, 4, 5, 6]$ which is sorted.
There can be other possible answers such as $[1, 2, 4, 3]$ .
There is only one correct order of swaps i.e $[2, 3, 1]$ .
1. First we perform the swap $2$ from the input i.e $(2, 1)$ , $p$ becomes $[3, 2, 4, 1]$ .
2. Then we perform the swap $3$ from the input i.e $(1, 3)$ , $p$ becomes $[4, 2, 3, 1]$ .
3. Finally we perform the swap $1$ from the input i.e $(1, 4)$ and $p$ becomes $[1, 2, 3, 4]$ which is sorted.
In the second example, $p = [6, 5, 1, 3, 2, 4]$ , $m = 4$ and the given swaps are $[(3, 1), (2, 5), (6, 3), (6, 4)]$ .
One possible correct order of swaps is $[4, 2, 1, 3]$ .
1. Perform the swap $4$ from the input i.e $(6, 4)$ , $p$ becomes $[6, 5, 1, 4, 2, 3]$ .
2. Perform the swap $2$ from the input i.e $(2, 5)$ , $p$ becomes $[6, 2, 1, 4, 5, 3]$ .
3. Perform the swap $1$ from the input i.e $(3, 1)$ , $p$ becomes $[1, 2, 6, 4, 5, 3]$ .
4. Perform the swap $3$ from the input i.e $(6, 3)$ and $p$ becomes $[1, 2, 3, 4, 5, 6]$ which is sorted.
There can be other possible answers such as $[1, 2, 4, 3]$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted