CF691D.Swaps in Permutation

普及+/提高

通过率:0%

时间限制:5.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a permutation of the numbers 1, 2, ..., n and m pairs of positions (a__j, b__j).

At each step you can choose a pair from the given positions and swap the numbers in that positions. What is the lexicographically maximal permutation one can get?

Let p and q be two permutations of the numbers 1, 2, ..., n. p is lexicographically smaller than the q if a number 1 ≤ i ≤ n exists, so p__k = q__k for 1 ≤ k < i and p__i < q__i.

给你一个 1,2,…,n1, 2, \dots, n 的排列,以及 mm 对位置 (aj,bj)(a_j, b_j)。

每一步中,你可以从给定的位置对中选择一对,并交换该对位置上的数字。问:通过若干次操作,你能得到的字典序最大的排列是什么?

设 pp 和 qq 是 1,2,…,n1, 2, \dots, n 的两个排列。若存在某个 1≤i≤n1 \le i \le n,使得对所有 1≤k<i1 \le k < i 都有 pk=qkp_k = q_k,且 pi<qip_i < q_i,则称 pp 的字典序小于 qq。

输入格式

The first line contains two integers n and m (1 ≤ n, m ≤ 106) — the length of the permutation p and the number of pairs of positions.

The second line contains n distinct integers p__i (1 ≤ p__i ≤ n) — the elements of the permutation p.

Each of the last m lines contains two integers (a__j, b__j) (1 ≤ a__j, b__j ≤ n) — the pairs of positions to swap. Note that you are given a positions, not the values to swap.

第一行包含两个整数 nn 和 mm(1≤n,m≤1061 \leq n, m \leq 10^6)—— 分别表示排列 pp 的长度以及待交换的位置对的数量。

第二行包含 nn 个互不相同的整数 pip_i(1≤pi≤n1 \leq p_i \leq n)—— 表示排列 pp 的元素。

接下来的 mm 行中,每行包含两个整数 (aj,bj)(a_j, b_j)(1≤aj,bj≤n1 \leq a_j, b_j \leq n)—— 表示需要交换的位置对。注意:此处给出的是位置,而非待交换的值。

输出格式

Print the only line with n distinct integers p'i (1 ≤ p'i ≤ n) — the lexicographically maximal permutation one can get.

输出唯一一行,包含 n 个互不相同的整数 p'i(1 ≤ p'i ≤ n),即所能得到的字典序最大的排列。

输入输出样例

  • 输入#1

    9 6
    1 2 3 4 5 6 7 8 9
    1 4
    4 7
    2 5
    5 8
    3 6
    6 9

    输出#1

    7 8 9 4 5 6 1 2 3

输入解题思路,AI测评打分。不知道怎么写?

首页