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
分析:一个联通体内的数从大到小排列就好;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e6+;
const int mod=1e6+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p%mod;p=p*p%mod;q>>=;}return f;}
int n,m,a[maxn],vis[maxn];
vi b[maxn],c;
set<int>p,q;
void dfs(int now)
{
vis[now]=;q.insert(now);c.pb(a[now]);
for(int x:b[now])if(!vis[x])dfs(x);
}
int main()
{
int i,j,k,t;
scanf("%d%d",&n,&m);
rep(i,,n)scanf("%d",&a[i]);
while(m--)
{
scanf("%d%d",&j,&k);b[j].pb(k),b[k].pb(j);
p.insert(j),p.insert(k);
}
for(int x:p)
{
if(!vis[x])
{
q.clear();c.clear();
dfs(x);
sort(c.rbegin(),c.rend());j=;
for(int y:q)a[y]=c[j++];
}
}
rep(i,,n)printf("%d ",a[i]);
//system ("pause");
return ;
}

Swaps in Permutation的更多相关文章

  1. Educational Codeforces Round 14 D. Swaps in Permutation 并查集

    D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are gi ...

  2. codeforces 691D D. Swaps in Permutation(dfs)

    题目链接: D. Swaps in Permutation time limit per test 5 seconds memory limit per test 256 megabytes inpu ...

  3. CodeForces 691D:Swaps in Permutation(并查集)

    http://codeforces.com/contest/691/problem/D D. Swaps in Permutation   You are given a permutation of ...

  4. Codeforces 691D Swaps in Permutation

    Time Limit:5000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Prac ...

  5. Educational Codeforces Round 14 D. Swaps in Permutation

    题目链接 分析:一些边把各个节点连接成了一颗颗树.因为每棵树上的边可以走任意次,所以不难想出要字典序最大,就是每棵树中数字大的放在树中节点编号比较小的位置. 我用了极为暴力的方法,先dfs每棵树,再用 ...

  6. codeforces 691D Swaps in Permutation DFS

    这个题刚开始我以为是每个交换只能用一次,然后一共m次操作 结果这个题的意思是操作数目不限,每个交换也可以无限次 所以可以交换的两个位置连边,只要两个位置连通,就可以呼唤 然后连通块内排序就好了 #in ...

  7. Educational Codeforces Round 14 D. Swaps in Permutation (并查集orDFS)

    题目链接:http://codeforces.com/problemset/problem/691/D 给你n个数,各不相同,范围是1到n.然后是m行数a和b,表示下标为a的数和下标为b的数可以交换无 ...

  8. 【搜索】【并查集】Codeforces 691D Swaps in Permutation

    题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...

  9. Educational Codeforces Round 14 D. Swaps in Permutation(并查集)

    题目链接:http://codeforces.com/contest/691/problem/D 题意: 题目给出一段序列,和m条关系,你可以无限次互相交换这m条关系 ,问这条序列字典序最大可以为多少 ...

随机推荐

  1. 数据库文件导入导出操作,以及赋予权限SQL语句

    1.导出数据库xxxx和tlog(经过测试,没有问题)# /data/mysql/bin/mysqldump -u root -ppassword qq9x | gzip > /home/xxx ...

  2. Python 2 中的编码

    在 Python 尤其是 Python2 中,编码问题是困扰开发者尤其初学者的一大问题.什么 Unicode/UTF-8/str ,又是 decode/encode 的,搞得人头都大了.其实不然,这有 ...

  3. 深入了解当前ETL的一些基本技术

    数据集成是把不同来源.格式和特点的数据在逻辑上或物理上有机地集中,从而为企业提供全 面的数据共享,是企业商务智能.数据仓库系统的重要组成部分.ETL是企业数据集成的概念出发,简要分析了当前ETL中用到 ...

  4. iOS拨打电话

    1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示NSMutableString * str=[[NSMutableString alloc] initWithFo ...

  5. array_walk与array_map 的不同 array_filter

      array_walk 主要是要对数组内的每个值进行操作,操作结果影响原来的数组 array_map主要是对数组中的值进行操作后返回数组,以得到一个新数组 wallk 可以没有返回值 map要有,因 ...

  6. Oracle小技术集锦

  7. 关于getchar()的知识

    char* s1 = "123",字符串"123"这段内存是只读的,就是说其内容不能改变///char *s 这个是指申请一个地址空间 记录一个地址 #incl ...

  8. php编译错误Note that the MySQL client library is not bundled anymore!

    Note that the MySQL client library is not bundled anymore! 解决方法. 1. 查看系统有没有安装mysql header find / -na ...

  9. UI篇—UITableview

    一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UITableView继承自UIScrollView,因此支 ...

  10. uCGUI的文字与数值显示方法

    uCGUI的数值显示非常的灵活方便,是制作LCD界面非常好的选择. 文字与数值显示的方法: 常用文本显示函数: void GUI_DispStringAt(const char GUI_FAR *s, ...