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

Description

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.

Sample Input

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
/*
我本来还以为每种操作只能操作一次,结果可以操作无穷次
“At each step you can choose a pair from the given positions and swap the numbers in that positions.”
怪我英语不好,┗( T﹏T )┛
DFS或并查集求连通块。
把每一条允许交换的位置看做是图中的一条边,画图会发现:一个连通块内的那些位置是可以任意交换的。
因此,只需找出连通块,每一块内的值从大到小排序就行。 */
#include <iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int team[],teampos[],a[];
bool vis[];
int cnt,n,m;
vector<int> s[];
void dfs(int k)
{
team[++cnt]=a[k];
teampos[cnt]=k;
vis[k]=;
for(int i=;i<s[k].size();i++)
if (!vis[s[k][i]]) dfs(s[k][i]);
}
bool cmp(int a,int b)
{
return a>b;
} int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=;i<=n;i++)
{scanf("%d",&a[i]);vis[i]=;s[i].clear();} for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
s[x].push_back(y);
s[y].push_back(x);
}
for(int i=;i<=n;i++)
if (!vis[i])
{
cnt=;
dfs(i);
sort(teampos+,teampos+cnt+);
sort(team+,team+cnt+,cmp);
for(int j=;j<=cnt;j++)
a[teampos[j]]=team[j];
}
for(int i=;i<n;i++)
printf("%d ",a[i]);
printf("%d\n",a[n]);
}
return ;
}

Codeforces 691D Swaps in Permutation的更多相关文章

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

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

  2. codeforces 691D Swaps in Permutation DFS

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

  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 D. Swaps in Permutation(dfs)

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

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

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

  6. Swaps in Permutation

    Swaps in Permutation You are given a permutation of the numbers 1, 2, ..., n and m pairs of position ...

  7. codeforces 691D(数据结构)

    D. Swaps in Permutation time limit per test 5 seconds memory limit per test 256 megabytes input stan ...

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

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

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

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

随机推荐

  1. 锅巴视频工作室 ----------------android端蓝牙测试demo--app

    android端蓝牙测试demo--app 这个是为一个客户做蓝牙项目时的一个测试demo,用来测试蓝牙单片机的收发情况,代码中没有做一些兼容性测试,请理解 锅巴视频工作室,专注于android视频相 ...

  2. 扔鸡蛋问题详解(Egg Dropping Puzzle)

    http://blog.csdn.net/joylnwang/article/details/6769160 经典的动态规划问题,题设是这样的:如果你有2颗鸡蛋,和一栋36层高的楼,现在你想知道在哪一 ...

  3. 使用vs2010打开VS2013的工程文件

    在开发团队,会出现vs工具使用版本的不一样的情况.我的电脑使用的是VS2010,可是其他人员使用的是vs2013. 要打开其他人员上传的工程文件,可以通过三种方式: 1.下载一个vs2013版本. 2 ...

  4. java回调机制(写的很好)

    本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17483273) 以前不理解什么叫回调,天天听人家说加一个回调方法啥的 ...

  5. 二十七、oracle 例外

    一.例外分类oracle将例外分为预定义例外.非预定义例外和自定义例外三种.1).预定义例外用于处理常见的oracle错误.2).非预定义例外用于处理预定义例外不能处理的例外.3).自定义例外用于处理 ...

  6. geekNews 学习总结

    1:下移隐藏,上移出现 //android.support.v4.widget.NestedScrollView nsvScroller; //FrameLayout llDetailBottom; ...

  7. iframe标签flash遮盖页面元素问题——wmode参数

    最近做项目过程中,遇到各种小问题,所以打算通过博客园来记录各问题的解决办法. 这篇的问题背景是这样子的:项目是用的AngularJS框架,在某个页面上用了wangEditor富文本编辑器插件(gith ...

  8. MySQL命令出现字符错误提示原因

    ERROR 2019 (HY000): Can't initialize character set gb2312 搞了好半天,MySQL都重装了两次,号悲剧... 之前设置了系统编码全都是UTF-8 ...

  9. 手机浏览器无法获取COOKIE的原因

    手机浏览器上无法使用cookie,肯能是 1. 浏览器禁用 COOKIE ,这个简单开启即可. 2. 可能是手机所在时区有问题,将COOKIE有效期设置更长时间测试下,在更改时区

  10. CodeForces 702E Analysis of Pathes in Functional Graph

    倍增预处理. 先看一下这张图的结构,因为出度都是$1$,所以路径是唯一的,又因为每个点都有出度,所以必然有环,也就是一直可以走下去. 接下来我们需要记录一些值便于询问: 设$t[i][j]$表示从$i ...