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相关推荐

  1. czl蒻蒟的OI之路14、15

    XJOI奋斗群蒻蒟群群赛15 RANK排名9 T1Fashion in Berland 已AC 题意 分析过程 给出题解 T2s-palindrome WA四次后AC 题意 分析过程 给出题解 T3E ...

  2. 2016区域赛前冲刺训练

    UPD 2016.10.23 shift-and (2题) Codeforces 训练 现在已经完成了: 191 [Codeforces Round #377] (6/6) Div 2 A Buy a ...

  3. 【C++】C++11 STL算法(七):排列操作(Permutation operations)、数值操作(Numeric operations)

    排列操作(Permutation operations) 一.is_permutation 1.原型: template< class ForwardIt1, class ForwardIt2 ...

  4. LeetCode 76. Minimum Window Substring / 567. Permutation in String

    76. Minimum Window Substring 典型Sliding Window的问题,维护一个区间,当区间满足要求则进行比较选择较小的字串,重新修改start位置. 思路虽然不难,但是如何 ...

  5. R语言使用lmPerm包应用于线性模型的置换方法(置换检验、permutation tests)、使用lm模型构建简单线性回归模型、使用lmp函数生成置换检验回归分析模型

    R语言使用lmPerm包应用于线性模型的置换方法(置换检验.permutation tests).使用lm模型构建简单线性回归模型.使用lmp函数生成置换检验回归分析模型(Permutation te ...

  6. R语言使用coin包应用于独立性问题的置换检验(permutation tests、响应变量是否独立于组、两个数值变量是独立的吗、两个分类变量是独立的吗)、以及coin包的常用置换检验函数

    R语言使用coin包应用于独立性问题的置换检验(permutation tests.响应变量是否独立于组.两个数值变量是独立的吗.两个分类变量是独立的吗).以及coin包的常用置换检验函数 目录

  7. R语言使用coin包应用于独立性问题的置换检验(permutation tests)、使用普通cor.test函数和置换近似spearman_test函数、检验变量的相关性的显著性

    R语言使用coin包应用于独立性问题的置换检验(permutation tests).使用普通cor.test函数和置换近似spearman_test函数.检验变量的相关性的显著性(correlati ...

  8. R语言使用coin包应用于分类变量独立性问题的置换检验(permutation tests)、使用普通卡方检验chisq.test函数和置换近似卡方检验chisq.test函数、检验分类变量的独立性

    R语言使用coin包应用于分类变量独立性问题的置换检验(permutation tests).使用普通卡方检验chisq.test函数和置换近似卡方检验chisq.test函数.检验分类变量的独立性( ...

  9. R语言置换检验(permutation tests、响应变量是否独立于组、两个数值变量是独立的吗、两个分类变量是独立的吗)、置换检验的基本步骤、R语言自助法Bootstrapping计算置信区间

    R语言置换检验(permutation tests.响应变量是否独立于组.两个数值变量是独立的吗.两个分类变量是独立的吗).置换检验的基本步骤.R语言自助法Bootstrapping.自助法计算单个统 ...

最新文章

  1. 贵州省新农合业务系统容灾技术支撑服务项目
  2. 白血病孕妇产子继母子双双安然
  3. 为什么说_br__标签需要闭合
  4. c语言实践教程实验题答案,C语言课后实验教程习题答案
  5. 有人去瑞幸咖啡java_瑞幸股价暴跌,门店竟然爆单了
  6. VS发布 错误 未能将文件 复制到
  7. MySql中的变量定义
  8. 千脑跳转页面下的安全分析
  9. Java 分页计算公式
  10. linux 编译java文件 执行class文件
  11. php 获取当前、两个月之前的日期
  12. c++实现植物大战僵尸修改器
  13. iOS 显示SVG文件的方法
  14. 怎么把已经初始化的字符数组设置为空?
  15. C. Chocolate Bunny(思维+规律)
  16. Compilation error 未完待续
  17. 2015 上海网赛 HDU5469 树分治
  18. python安装dlib遇到的错误:AttributeError:module ‘dlib’ has no attribute ‘get_frontal_face_detector’问题解决
  19. vue+openlayers图形交互,实现多边形绘制、编辑和保存
  20. 计算两点方向的方位角

热门文章

  1. 2021-09-252021年中式烹调师(中级)考试技巧及中式烹调师(中级)证考试
  2. 祖传治癌奇方 仅供参考 转自网络
  3. nginx配置+redis使用以及同步
  4. 【无标题】三星Xpress M2020打印机刷免芯片
  5. sqlite优化简单分析
  6. Unicode和Python的中文处理(收藏)
  7. go语言爬虫解析html,Python爬虫【如何爬取内容(html)和解析内容】-Go语言中文社区...
  8. zb system login.php,zblog 修改后台登陆地址的教程
  9. 中国矿业大学考研计算机应用技术,2020中国矿业大学计算机专业课考试科目变动...
  10. 云班课python答案_云班课 答案 脚本