CodeForces 691D:Swaps in Permutation(并查集)
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.
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.
Print the only line with n distinct integers p'i (1 ≤ p'i ≤ n) — the lexicographically maximal permutation one can get.
9 6
1 2 3 4 5 6 7 8 9
1 4
4 7
2 5
5 8
3 6
6 9
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(并查集)的更多相关文章
- 【搜索】【并查集】Codeforces 691D Swaps in Permutation
题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...
- Educational Codeforces Round 14 D. Swaps in Permutation 并查集
D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are gi ...
- Educational Codeforces Round 14 D. Swaps in Permutation (并查集orDFS)
题目链接:http://codeforces.com/problemset/problem/691/D 给你n个数,各不相同,范围是1到n.然后是m行数a和b,表示下标为a的数和下标为b的数可以交换无 ...
- Codeforces 691D Swaps in Permutation
Time Limit:5000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Status Prac ...
- codeforces 691D Swaps in Permutation DFS
这个题刚开始我以为是每个交换只能用一次,然后一共m次操作 结果这个题的意思是操作数目不限,每个交换也可以无限次 所以可以交换的两个位置连边,只要两个位置连通,就可以呼唤 然后连通块内排序就好了 #in ...
- 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 ...
- Codeforces 699D Fix a Tree 并查集
原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种 ...
- Codeforces 731C:Socks(并查集)
http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...
- codeforces 400D Dima and Bacteria 并查集+floyd
题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...
- Codeforces 1027F Session in BSU - 并查集
题目传送门 传送门I 传送门II 传送门III 题目大意 有$n$门科目有考试,第$i$门科目有两场考试,时间分别在$a_i, b_i\ \ (a_i < b_i)$,要求每门科目至少参加 ...
随机推荐
- jquery表单过滤器
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- 同时使用SpringJUnit4ClassRunner和Parameterized进行参数化
标题实际上是个不可能完成的任务,因为我们只能用一个Runwith注解,而且只能写一个类,但是我们可以曲线救国,插入下方的5到14行就可以注入了 @ContextConfiguration(classe ...
- 使注解@ContextConfiguration同时支持locations和classes
@Configuration @ImportResource("classpath:META-INF/dataContext.xml") class TestConfig { } ...
- Win10《芒果TV》发布两周年纪念特别献礼,陪你度国庆,好礼送不停
2015年芒果TV和微软中国联姻,在Windows10发布之际,共同宣告了Win10版<芒果TV>的诞生,第一个版本于2015年9月30日登陆Windows商店,历经28次迭代,现在她两岁 ...
- git pull和fetch的区别
详解git pull和git fetch的区别: - weixin_41975655的博客 - CSDN博客 https://blog.csdn.net/weixin_41975655/article ...
- flume本地调试
本机idea远程调试flume:https://blog.csdn.net/u012373815/article/details/60601118 遇到 [root@hadoop02 bin]# ./ ...
- C#字符串类型
C#字符串类型(string)是一种引用类型,是System.String的别名,表示Unicode字符串. 两种表示方法: 1.“C#” 直接用双引号括起来. 2.使用@,@“c:\test”,可以 ...
- ef 查询总结
1.Linq 查询两张表:a表和b表,要得到的数据是a表数据b表没有 例如:a表有5条数据1,2,3,4,5:b表有2条数据1,3:那么就用dataGridView1输出2,4,5:link语句要怎么 ...
- 在Windows IoT上使用网络摄像头
在树莓派上可以使用它官方标配的摄像头,但是这个摄像头似乎不能被Windows IoT识别和使用.但是,可以在树莓派的USB口上插入任意型号的摄像头,就可以实现树莓派的拍摄功能. 关于摄像头的寻找和拍摄 ...
- GCC链接库的一个坑:动态库存在却提示未定义动态库的函数
背景 在GCC中已经指定链接库,然而编译时却提示动态库函数未定义! 测试出现的错误提示如下: [GMPY@13:48 tmp]$gcc -o test -L. -lmylib test.c /tmp/ ...