QOJ.ac

QOJ

시간 제한: 1 s 메모리 제한: 256 MB 총점: 100

#4805. 格莱美排序

통계

Grammy 拥有一个包含 $n$ 个顶点的连通无向图 $G$,顶点编号为 $1, 2, \dots, n$。其中有两个特殊顶点 $A$ 和 $B$。每个顶点 $i$ 上都写有一个数字 $p_i$,其中 $p_1, p_2, \dots, p_n$ 是 $1, 2, \dots, n$ 的一个排列。

Grammy 认为顶点上的这些数字太混乱了。她想要重新排列这些数字,使得对于每个顶点 $x$,都存在一条满足以下条件的路径: 路径从 $A$ 开始,到 $B$ 结束。 路径包含顶点 $x$。 * 路径上的数字严格递增。

遗憾的是,在每次操作中,Grammy 只能选择一条从 $A$ 开始到任意顶点的简单路径,然后将该简单路径上的数字向起点方向移动一个位置,并将第一个数字放到最后一个位置。形式化地,如果 Grammy 选择的简单路径上的顶点从起点到终点依次包含数字 $a_1, a_2, \dots, a_{k-1}, a_k$,那么在 Grammy 的操作后,这些顶点将包含 $a_2, a_3, \dots, a_k, a_1$。

此外,Grammy 最多只能进行 $10\,000$ 次操作。

Grammy 对如何解决这个问题毫无头绪,所以她向你寻求帮助。请帮助 Grammy 确定她是否能按要求重新排列数字。如果存在,你还需要输出一个解决方案。

输入格式

第一行包含四个整数 $n, m, A, B$ ($2 \le n \le 1000, 1 \le m \le 2000, 1 \le A, B \le n, A \neq B$)。 第二行包含 $n$ 个整数 $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$)。保证 $p_1, p_2, \dots, p_n$ 是一个排列。 接下来的 $m$ 行中,每行包含两个整数 $u_i$ 和 $v_i$ ($1 \le u_i, v_i \le n, u_i \neq v_i$),表示 $u_i$ 和 $v_i$ 之间有一条双向边。保证图是连通的,且任意两个顶点之间最多只有一条边。

输出格式

如果 Grammy 无法正确重新排列数字,输出 “-1”(不含引号)。 否则,在第一行输出一个整数 $op$ ($0 \le op \le 10\,000$),表示要执行的操作次数。 在接下来的 $op$ 行中,首先输出一个整数 $k$,表示所选简单路径上的顶点数。然后输出 $k$ 个整数 $x_1, x_2, \dots, x_k$ ($x_1 = A, 1 \le x_i \le n$),表示简单路径上的顶点。这些 $x_i$ 应当互不相同,且在 $G$ 中构成一条路径。

可以证明,如果图 $G$ 可以被正确重排,则存在一个不超过 $10\,000$ 次操作的解。 注意,你不必最小化 $op$。如果存在多个解,输出其中任意一个即可。

样例

样例输入 1

5 6 1 2
1 2 3 4 5
1 3
2 3
1 4
2 4
1 5
3 5

样例输出 1

7
4 1 3 2 4
3 1 3 2
3 1 3 5
4 1 3 2 4
3 1 3 2
2 1 3
1 1

样例输入 2

4 3 1 2
1 4 2 3
1 4
2 4
3 4

样例输出 2

-1

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#512Editorial Open集训队作业 解题报告 by 何宇翔Qingyu2026-01-02 21:34:04View PDF

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.