AtCoder Regular Contest++ 216
E
交换或反转
Swap or Reverse
AtCoder
未评级
原题
← D→

交换或反转AI (CI翻译)

Problem Statement

题目描述

AI (CI翻译)

You are given a permutation $P = (P_1, P_2, \ldots, P_N)$ of $(1, 2, \ldots, N)$ and a set of pairs of integers $S = \lbrace (x_1, y_1), (x_2, y_2), \ldots, (x_M, y_M) \rbrace$.

给定一个 $(1, 2, \ldots, N)$ 的排列 $P = (P_1, P_2, \ldots, P_N)$,以及一个整数对集合 $S = \lbrace (x_1, y_1), (x_2, y_2), \ldots, (x_M, y_M) \rbrace$。AI (CI翻译)

You can perform the following two types of operations any number of times in any order.

你可以按照任意顺序执行以下两种操作任意次。AI (CI翻译)

  • Choose $(l, r)\ (1 \leq l < r \leq N)$ such that $(P_l, P_r) \in S$ or $(P_r, P_l) \in S$, and swap the $l$-th and $r$-th elements of $P$. That is, replace $P$ with $(P_1, \ldots, P_{l-1}, P_r, P_{l+1}, \ldots, P_{r-1}, P_l, P_{r+1}, \ldots, P_N)$.
  • Choose $(l, r)\ (1 \leq l < r \leq N)$ such that $(P_l, P_r) \in S$ or $(P_r, P_l) \in S$, and reverse the elements from the $l$-th to the $r$-th of $P$. That is, replace $P$ with $(P_1, \ldots, P_{l-1}, P_r, P_{r-1}, \ldots, P_{l+1}, P_l, P_{r+1}, \ldots, P_N)$.
  • 选择 $(l, r)\ (1 \leq l < r \leq N)$,满足 $(P_l, P_r) \in S$ 或 $(P_r, P_l) \in S$,然后交换 $P$ 的第 $l$ 个和第 $r$ 个元素。也就是说,将 $P$ 替换为 $(P_1, \ldots, P_{l-1}, P_r, P_{l+1}, \ldots, P_{r-1}, P_l, P_{r+1}, \ldots, P_N)$。
  • 选择 $(l, r)\ (1 \leq l < r \leq N)$,满足 $(P_l, P_r) \in S$ 或 $(P_r, P_l) \in S$,然后反转 $P$ 中从第 $l$ 个到第 $r$ 个的元素。也就是说,将 $P$ 替换为 $(P_1, \ldots, P_{l-1}, P_r, P_{r-1}, \ldots, P_{l+1}, P_l, P_{r+1}, \ldots, P_N)$。
AI (CI翻译)

Find the lexicographically smallest permutation obtainable by the operations.

求通过上述操作可以得到的字典序最小排列。AI (CI翻译)

You are given $T$ test cases; solve each of them.

给定 $T$ 个测试用例,请分别求出每个测试用例的答案。AI (CI翻译)

Constraints

约束条件

AI (CI翻译)
  • $1 \leq T \leq 3 \times 10^4$
  • $2 \leq N \leq 2 \times 10^5$
  • $1 \leq M \leq 2 \times 10^5$
  • $P$ is a permutation of $(1, 2, \ldots, N)$.
  • $1 \leq x_i < y_i \leq N$
  • $(x_i, y_i) \neq (x_j, y_j)$ if $i \neq j$.
  • The sum of $N$ over all test cases is at most $2 \times 10^5$.
  • The sum of $M$ over all test cases is at most $2 \times 10^5$.
  • All input values are integers.
  • $1 \leq T \leq 3 \times 10^4$
  • $2 \leq N \leq 2 \times 10^5$
  • $1 \leq M \leq 2 \times 10^5$
  • $P$ 是 $(1, 2, \ldots, N)$ 的排列。
  • $1 \leq x_i < y_i \leq N$
  • 当 $i \neq j$ 时,$(x_i, y_i) \neq (x_j, y_j)$。
  • 所有测试用例的 $N$ 之和不超过 $2 \times 10^5$。
  • 所有测试用例的 $M$ 之和不超过 $2 \times 10^5$。
  • 所有输入值均为整数。
AI (CI翻译)

Input

输入格式

AI (CI翻译)

The input is given from Standard Input in the following format:

输入从标准输入给出,格式如下:AI (CI翻译)

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
AI (CI翻译)

Each test case is given in the following format:

每个测试用例的格式如下:AI (CI翻译)

N M
P_1 P_2 \ldots P_N
x_1 y_1
x_2 y_2
\vdots
x_M y_M

N M
P_1 P_2 \ldots P_N
x_1 y_1
x_2 y_2
\vdots
x_M y_M
AI (CI翻译)

Output

输出格式

AI (CI翻译)

Output $T$ lines. The $i$-th line should contain the answer to $\mathrm{case}_i$.

输出 $T$ 行。第 $i$ 行应包含 $\mathrm{case}_i$ 的答案。AI (CI翻译)

样例

Input #1
2
6 2
1 3 2 5 4 6
1 4
2 5
8 5
7 3 2 8 6 5 1 4
1 8
2 5
3 4
7 8
5 6
Output #1
1 2 5 3 4 6
1 2 3 7 5 6 8 4
For the first test case, you can obtain $P = (1, 2, 5, 3, 4, 6)$ by performing the operations as follows. - Initially, $P = (1, 3, 2, 5, 4, 6)$. - Reverse the elements from the first to the fifth with $(l, r) = (1, 5)\ ((P_l, P_r) = (1, 4) \in S)$. $P$ becomes $(4, 5, 2, 3, 1, 6)$. - Swap the second and third elements with $(l, r) = (2, 3)\ ((P_r, P_l) = (2, 5) \in S)$. $P$ becomes $(4, 2, 5, 3, 1, 6)$. - Swap the first and fifth elements with $(l, r) = (1, 5)\ ((P_r, P_l) = (1, 4) \in S)$. $P$ becomes $(1, 2, 5, 3, 4, 6)$. This is the lexicographically smallest permutation obtainable by the operations.
题目信息
题号
arc216_e
时间限制
5 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。