题目链接:

D. Swaps in Permutation

time limit per test

5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

题意:

给一个数列,现在可以交换ai和bi,问能得到的最大的字典序的数列是什么;

思路:

把能交换位置的数放在同一个集合里;可以dfs找到,然后把这些数和位置排序对应就好了;不知道cf的数据怎么了;

AC代码:
#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1e6+;
const int maxn=;
const double eps=1e-; int n,m,p[N],a[N],b[N],ans[N],cnt,vis[N];
vector<int>ve[N];
void dfs(int x)
{
vis[x]=;
a[++cnt]=p[x];
b[cnt]=x;
int len =ve[x].size();
For(i,,len-)
{
int y =ve[x][i];
if(!vis[y]) dfs(y);
}
}
int cmp(int x,int y)
{
return x>y;
}
void solve()
{
sort(a+,a+cnt+,cmp);
sort(b+,b+cnt+);
For(i,,cnt)ans[b[i]]=a[i];
} int main()
{ read(n);read(m);
For(i,,n)read(p[i]);
int u,v;
For(i,,m)
{
read(u);read(v);
ve[u].push_back(v);
ve[v].push_back(u);
}
For(i,,n)
{
if(!vis[i])
{
cnt=;
dfs(i);
solve();
}
}
For(i,,n)printf("%d ",ans[i]); return ;
}

最新文章

  1. &lt;&lt;Design Patterns&gt;&gt; Gang of Four
  2. Ubuntu grub引导修复
  3. IOSUIcontrol事件
  4. web app 的技术参考 -- 来自 【百度移动建站指南】
  5. SQL里IN的用法以及优化
  6. BZOJ1895: Pku3580 supermemo
  7. DS1302-演示代码
  8. Html表格自动换行
  9. XAML 概述
  10. Aix 光盘软件包安装
  11. Java Executor 框架
  12. [译]如何在Web开发中使用Python
  13. 嵌入式LINUX环境下视频采集知识
  14. react-native-android之初次相识
  15. Swift之GCD使用指南1
  16. java模板设计模式
  17. js复制文本内容到剪贴板
  18. Hadoop源码分析之FileSystem抽象文件系统
  19. gdb 调试入门,大牛写的高质量指南
  20. [LeetCode] 35. Search Insert Position_Easy tag: Binary Search

热门文章

  1. mysql date_add日期函数的使用
  2. Codeforces 515E Drazil and Park (ST表)
  3. [TJOI2019]唱、跳、rap和篮球_生成函数_容斥原理_ntt
  4. 在springboot项目中获取pom.xml中的信息
  5. jquery提示消息,简单通用
  6. Codeforces Round #266 (Div. 2) C. Number of Ways
  7. aSmack连接server异常smack.SmackException$ ConnectionException thrown by XMPPConnection.connect();
  8. svn 创建分支、切换分支 及 合并分支 操作
  9. 安卓2.3 js解析问题 split()
  10. Codeforces Round #262 (Div. 2)460A. Vasya and Socks(简单数学题)