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. ubuntu配置LAMP

    1.安装Apache sudo apt-get install apache2 如果显示未找到软件则执行sudo apt-get update(建议使用ubuntu正在支持的系统版本,不在支持的版本可 ...

  2. Scala Apply

    class ApplyTest{ //一定要写(),不加括号就报错. def apply() = println("Into Spark!") def havaAtry(){ pr ...

  3. GCD系列 之(一):基本概念和Dispatch Queue

    参考学习https://www.dreamingwish.com/article/grand-central-dispatch-basic-1.html系列文章,貌似也是翻译自他处的.觉得非常完整,就 ...

  4. HDU 5073 Galaxy (2014 Anshan D简单数学)

    HDU 5073 Galaxy (2014 Anshan D简单数学) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5073 Description G ...

  5. Multi-Objective Data Placement for Multi-Cloud Socially Aware Services---INFOCOM 2014

    [标题] [作者] [来源] [对本文评价] [why] 存在的问题 [how] [不足] assumption in future work [相关方法或论文] [重点提示] [其它]

  6. jsvc 以daemon方式运行tomcat

    原理: 使用jsvc来运行服务,没有了默认8005的shutdown端口: 主进程pid为1,fork 2个进程 运行方式参考:http://commons.apache.org/proper/com ...

  7. JUnit01Eclipse添加JUnite

    JUnit 傻瓜教程 添加一個java工程 點擊右鍵選擇Properties 创建一个source folder 目的就是把测试类和被测试的类分开 添加一个类  加法 乘法 public class ...

  8. Base64编码 图片与base64编码互转

    package com.education.util; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import jav ...

  9. 微信小程序学习总结(1)

    1.一个小程序文件最基本包含app.js.app.json.app.wxss和pages文件夹结构,pages中的页面必须要在app.json的pages项进行配置,app.json也提供窗口表现,t ...

  10. 微信小程序入门——Mustache语法学习

    微信小程序中用到了大量Mustache语法,特发此文学习一下 1.简单的变量调换:{{name}} 1 var data = { "name": "Willy" ...