http://codeforces.com/contest/691/problem/D

D. 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, ..., n. p is lexicographically smaller than the q if a number 1 ≤ i ≤ n exists, so pk = qkfor 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

题意:给出一个含n个元素的序列,再给出m个可以无限次互相交换位置的下标,求出交换以后让整个序列的字典序最大。
思路:用并查集分出可以交换位置的下标集合,然后用vector装每个集合的下标和元素,然后每次查找集合的根节点的时候对每个集合的元素排序一下就好了。
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define N 1000005
vector <int> num[N];
vector <int> pos[N];
int p[N],a[N],ans[N];
int fa[N]; int Find(int x)
{
if(x==fa[x]) return x;
return fa[x]=Find(fa[x]);
} void Merge(int x,int y)
{
x=Find(x),y=Find(y);
if(x==y) return ;
fa[x]=y;
} int main()
{
int n,m;
cin>>n>>m;
for(int i=;i<=n;i++){
fa[i]=i;
scanf("%d",&a[i]);
p[i]=i;
}
for(int i=;i<=m;i++){
int a,b;
scanf("%d%d",&a,&b);
Merge(a,b);
}
for(int i=;i<=n;i++){
int x=Find(i);
pos[x].push_back(i);
num[x].push_back(-a[i]);
//因为要从大到小排序,而默认的排序是从小到大,所以先取负后面再取正
}
for(int i=;i<=n;i++){
sort(num[i].begin(),num[i].end());
for(int j=;j<pos[i].size();j++){
ans[pos[i][j]]=-num[i][j];
}
}
for(int i=;i<=n;i++){
printf("%d ",ans[i]);
}
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. Educational Codeforces Round 14 D. Swaps in Permutation 并查集

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

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

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

  4. Codeforces 691D Swaps in Permutation

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

  5. codeforces 691D Swaps in Permutation DFS

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

  6. CF 500 B. New Year Permutation 并查集

    User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permut ...

  7. Codeforces 699D Fix a Tree 并查集

    原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种 ...

  8. Codeforces 731C:Socks(并查集)

    http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...

  9. codeforces 400D Dima and Bacteria 并查集+floyd

    题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...

  10. Codeforces 1027F Session in BSU - 并查集

    题目传送门 传送门I 传送门II 传送门III 题目大意 有$n​$门科目有考试,第$i​$门科目有两场考试,时间分别在$a_i, b_i\ \ (a_i < b_i)​$,要求每门科目至少参加 ...

随机推荐

  1. MyBatis Generator 详解 专题

    idea中有plugin可提高效率: http://www.henryxi.com/use-idea-mybatis-plugin-generate-mapper-files eg: <?xml ...

  2. BZOJ 3329 Xorequ 数字DP+矩阵乘法

    标题效果:特定n,乞讨[1,n]内[1,2^n]差多少x满足x^3x=2x x^3x=2x相当于x^2x = 3x 和3x=x+2x 和2x=x<<1 因此x满足条件IFFx&(x ...

  3. 2-21-源码编译安装LAMP

      编译安装LAMP所需要及其所使用的源码版本: httpd version:httpd-2.4.16 apr version:apr-1.5.2 pcre version:pcre-8.37 apr ...

  4. jquery 复选框操作-prop()的使用

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  5. WPF 创建无边框的圆角窗口

    原文:WPF 创建无边框的圆角窗口 如题所述,在WPF中要创建一个没有边框且为圆角的窗体,有如下几步工作要进行: 第一步:去掉窗体默认样式的边框 首先将窗体的背景设为透明,将允许透明的属性设置为Tru ...

  6. NUGET源不存在,安装Nuget包提示“本地源不存在”

    困扰了两天的问题,终于找到原因了.因此来这里记录一下~ 前两天写项目时,要从NUGET上安装个第三方库,但不管是从可视化的管理器还是管理器控制台安装,都提示“本地源‘*******’不存在”.然后到网 ...

  7. C# 生成txt日志文件

    /// <summary> /// 创建日志文件,每天一个 /// </summary> /// <param name="logContent"&g ...

  8. windows media player 播放视频

    一.新建windows应用程序项目,添加vedioForm窗体 二.在com组件中找到windows media player,添加引用 三.代码如下: public partial class Ve ...

  9. shell中select、case的使用

    case和select结构在技术上说并不是循环, 因为它们并不对可执行代码块进行迭代. 但是和循环相似的是, 它们也依靠在代码块顶部或底部的条件判断来决定程序的分支. select   select结 ...

  10. MySQL 其它基本操作

    索引 所谓索引,就是类似于书的目录,目的也类似,都是为了提高检索速度.ALTER TABLE <表名> ADD INDEX <索引名(列名)>;或者CREATE INDEX & ...