Swaps in Permutation

2024-01-29 12:58
文章标签 permutation swaps

本文主要是介绍Swaps in Permutation,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

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, ..., np is lexicographically smaller than the q if a number 1 ≤ i ≤ n exists, sopk = qk for 1 ≤ k < i and pi < qi.

Input

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 pi (1 ≤ pi ≤ n) — the elements of the permutation p.

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

Output

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

Example
input
9 6
1 2 3 4 5 6 7 8 9
1 4
4 7
2 5
5 8
3 6
6 9
output
7 8 9 4 5 6 1 2 3

给你n个数
下m行,每行两个数,这两个位置的数可以无限次的交换
让你输出一个字典序最大的排序


因为1,4可以无限次的交换,4,7也可以无限次的交换,所以1,7也可以交换,这样可以想到用并查集把他们划为一类,

然后再用优先队列把同一个集合的存进去,这样输出的时候就能保证大的在前面。


#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>using namespace std;int f[1000010];
int a[1000010];
int find(int x)
{if(x == f[x])return f[x];elsereturn f[x] = find(f[x]);
}
priority_queue<int> q[1000010];int main(void)
{int n,m,i;while(scanf("%d%d",&n,&m)==2){for(i=0;i<=n;i++)f[i] = i;for(i=1;i<=n;i++)scanf("%d",&a[i]);for(i=1;i<=m;i++){int t1,t2;scanf("%d%d",&t1,&t2);int x = find(t1);int y = find(t2);if(x != y)f[x] = y;}for(i=1;i<=n;i++)q[f[find(i)]].push(a[i]);for(i=1;i<=n;i++){printf("%d ",q[f[i]].top());q[f[i]].pop();}}
}



这篇关于Swaps in Permutation的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/656976

相关文章

【ZOJ】3874 Permutation Graph 【FFT+CDQ分治】

传送门:【ZOJ】3874 Permutation Graph 题目分析: 容易知道一个个连通块内部的标号都是连续的,否则一定会有另一个连通块向这个连通块建边,或者这个连通块向另一个连通块建边。而且从左到右左边的连通块内最大的标号小于右边连通块内最小的标号。 然后我们可以构造dp方程: dp[n]=n!−i!∗dp[n−i] \qquad \qquad dp[n] = n! - i! *

CodeForces 425A Sereja and Swaps

题意: 一串数字  最多可以做k次交换数字  求  最大连续和是多少 思路: n^2暴力枚举所有区间  那么如果要换数字  一定是从区间外拿大数换区间内的小数  优先队列可以完成操作 代码: #include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;

LeetCode 31 Next Permutation

题意: 给出一串数字,求该排列的下一个排列。如果该排列为字典序最大排列,则输出字典序最小排列。 思路: 首先C++里面有求下一个排列的函数next_permutation,该函数返回0即表示当前排列字典序最大。 如果要自己动手实现,那么就考虑“如何找到比当前排列大1的排列”。 假设排列A(aaaXddd)比排列B(aaaYfff)大1,a部分为两排列相同部分,X与Y表示最靠左边不同

String Permutation

Given two strings, write a method to decide if one is a permutation of the other. Example Example 1:Input: "abcd", "bcad"Output: TrueExample 2:Input: "aac", "abc"Output: False 思路:count比较一下就可以;

Codeforces Round #252(Div. 2) 441D. Valera and Swaps 置换群

D. Valera and Swaps time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output A permutation p of length n is a sequence of distinc

numpy.random中的shuffle和permutation

shuffle: 沿着第一个axis打乱子数组的顺序,但是内容不变,相当于沿着第一个axis把array切成n个sub-array,然后打乱sub-array的顺序。(如果只有一维就只打乱元素) >>> arr = np.arange(9).reshape((3, 3))>>> np.random.shuffle(arr)>>> arrarray([[3, 4, 5],[6, 7, 8]

numpy.random.permutation

随机排列一个序列,返回一个排列的序列。 >>> np.random.permutation(10) array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6]) >>> np.random.permutation([1, 4, 9, 12, 15]) array([15,  1,  9,  4, 12]) >>> arr = np.arange(9).reshape((3, 3))

Java实现STL中的全排列函数next_permutation()

目录 一、引言 二、全排列函数next_permutation() 三、next_permutation()的使用 四、Java实现next_permutation() 五、使用next_permutation()实现全排列 一、引言 相信很多小伙伴们都做过全排列的算法题,输入一个n,输出1~n的全排列。对于这个问题,最经典的是实现方法应该是通过回溯实现 。 代码如下 i

数据分析:置换检验Permutation Test

欢迎大家关注全网生信学习者系列: WX公zhong号:生信学习者Xiao hong书:生信学习者知hu:生信学习者CDSN:生信学习者2 介绍 置换检验是一种非参数统计方法,它不依赖于数据的分布形态,因此特别适用于小样本数据集,尤其是当样本总体分布未知或不符合传统参数检验的假设条件时。置换检验的基本思想是通过随机置换样本来评估观察到的统计量是否显著不同于随机情况下的预期值。最初真正认识置换检

博弈论+递推+调和级数枚举,CF 1033C - Permutation Game

一、题目 1、题目描述 2、输入输出 2.1输入 2.2输出 3、原题链接 1033C - Permutation Game 二、解题报告 1、思路分析 我们考虑一个位置符合什么条件可以必胜? 如果可以跳到一个必败的位置 考虑最大的格子一定是必败 而每个格子只能跳到比自己大的格子 于是我们就可以倒序处理状态 对于每个格子枚举比自己大