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, …, n. p is lexicographically smaller than the q if a number 1 ≤ i ≤ n exists, so pk = 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 a positions, 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
#include <iostream>
#include <queue>using namespace std;const int N=1e6+10;priority_queue<int>ans[N];
int a[N],pre[N];struct DSU
{void Init(int n){for(int i=1;i<=n;i++) pre[i]=i;}int Find(int x){return x==pre[x] ? x:pre[x]=Find(pre[x]);}void Union(int a,int b){int x=Find(a);int y=Find(b);if(x==y) return;pre[y]=x;}
}dsu;int main()
{int n,m;while(cin>>n>>m){dsu.Init(n);for(int i=1;i<=n;i++) cin>>a[i];int x,y;for(int i=1;i<=m;i++){cin>>x>>y;dsu.Union(x,y);}for(int i=1;i<=n;i++) ans[pre[dsu.Find(i)]].push(a[i]);for(int i=1;i<=n;i++){cout<<ans[pre[i]].top()<<(i==n ? '\n':' ');ans[pre[i]].pop();}}return 0;
}
Swaps in Permutation相关推荐
- czl蒻蒟的OI之路14、15
XJOI奋斗群蒻蒟群群赛15 RANK排名9 T1Fashion in Berland 已AC 题意 分析过程 给出题解 T2s-palindrome WA四次后AC 题意 分析过程 给出题解 T3E ...
- 2016区域赛前冲刺训练
UPD 2016.10.23 shift-and (2题) Codeforces 训练 现在已经完成了: 191 [Codeforces Round #377] (6/6) Div 2 A Buy a ...
- 【C++】C++11 STL算法(七):排列操作(Permutation operations)、数值操作(Numeric operations)
排列操作(Permutation operations) 一.is_permutation 1.原型: template< class ForwardIt1, class ForwardIt2 ...
- LeetCode 76. Minimum Window Substring / 567. Permutation in String
76. Minimum Window Substring 典型Sliding Window的问题,维护一个区间,当区间满足要求则进行比较选择较小的字串,重新修改start位置. 思路虽然不难,但是如何 ...
- R语言使用lmPerm包应用于线性模型的置换方法(置换检验、permutation tests)、使用lm模型构建简单线性回归模型、使用lmp函数生成置换检验回归分析模型
R语言使用lmPerm包应用于线性模型的置换方法(置换检验.permutation tests).使用lm模型构建简单线性回归模型.使用lmp函数生成置换检验回归分析模型(Permutation te ...
- R语言使用coin包应用于独立性问题的置换检验(permutation tests、响应变量是否独立于组、两个数值变量是独立的吗、两个分类变量是独立的吗)、以及coin包的常用置换检验函数
R语言使用coin包应用于独立性问题的置换检验(permutation tests.响应变量是否独立于组.两个数值变量是独立的吗.两个分类变量是独立的吗).以及coin包的常用置换检验函数 目录
- R语言使用coin包应用于独立性问题的置换检验(permutation tests)、使用普通cor.test函数和置换近似spearman_test函数、检验变量的相关性的显著性
R语言使用coin包应用于独立性问题的置换检验(permutation tests).使用普通cor.test函数和置换近似spearman_test函数.检验变量的相关性的显著性(correlati ...
- R语言使用coin包应用于分类变量独立性问题的置换检验(permutation tests)、使用普通卡方检验chisq.test函数和置换近似卡方检验chisq.test函数、检验分类变量的独立性
R语言使用coin包应用于分类变量独立性问题的置换检验(permutation tests).使用普通卡方检验chisq.test函数和置换近似卡方检验chisq.test函数.检验分类变量的独立性( ...
- R语言置换检验(permutation tests、响应变量是否独立于组、两个数值变量是独立的吗、两个分类变量是独立的吗)、置换检验的基本步骤、R语言自助法Bootstrapping计算置信区间
R语言置换检验(permutation tests.响应变量是否独立于组.两个数值变量是独立的吗.两个分类变量是独立的吗).置换检验的基本步骤.R语言自助法Bootstrapping.自助法计算单个统 ...
最新文章
- asp.net多图片上传案例_会计小明的故事-成本核算案例篇
- PDF文件修改后,保存时出现:文档无法保存。读取文档时出现问题(135)
- chrome表单自动填充去掉input黄色背景
- CocoaPod 使用之后知后觉
- 设计模式(五)--工厂模式汇总
- 关于VCP(Virtual Com Port)拓展的调试经历(一)
- php图片旋转显示不出来的,php – 我服务的图像不正确,它们都显示为旋转90度
- Could not load the btn_020.disable.png image referenced from a nib in the bundle with identifier ...
- 查找重复文件_快速查找、删除重复图片及文件!
- FFmpeg API 变更记录
- R语言:循环读取相似文件名的文件:
- 清华大学计算机刘旭鑫,助学服务 | 走近广东狮子会客家希望班高考“学霸”刘旭鑫、程春花同学...
- 单像空间后方交会模型
- SAS,软件使用中reg报错/gplot错误的解决方法。
- 数据分析(1):对比分析法
- i217lm网卡驱动linux,【电脑不能上网怎么安装网卡驱动】i217lm网卡驱动xp
- 【Earth Engine】基于GEE对季节性地物进行分类(多源数据叠图+监督分类)
- 【性能测试】性能测试指标TPS(Transaction per Second)
- 使用 es6 class类创建对象 在其他页面 用vue引用报错 is not a constructor
- 嵌入式开发 | 单片机能用C++开发吗?