Educational Codeforces Round 14 D. Swaps in Permutation 并查集
D. Swaps in Permutation
题目连接:
http://www.codeforces.com/contest/691/problem/D
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, ..., n. p is lexicographically smaller than the q if a number 1 ≤ i ≤ n exists, so pk = 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 a positions, not the values to swap.The only line contains the positive decimal number x. The length of the line will not exceed 106. Note that you are given too large number, so you can't use standard built-in data types "float", "double" and other.
Output
Print the only line with n distinct integers p'i (1 ≤ p'i ≤ n) — the lexicographically maximal permutation one can get.
Sample Input
9 6
1 2 3 4 5 6 7 8 9
1 4
4 7
2 5
5 8
3 6
6 9
Sample Output
7 8 9 4 5 6 1 2 3
Hint
题意
给你一个排列,和m个可以交换的关系
然后你只能交换这m对关系,你可以交换无限次
问你,你能够得到的最大字典序的串是什么
题解:
把能够交换得到的位置用并查集维护,然后对于每个位置,把能够放的最大值扔上去就好了
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
int fa[maxn],n,m;
int fi(int x){
if(x==fa[x])return x;
return fa[x]=fi(fa[x]);
}
vector<int> E[maxn];
int t[maxn];
bool cmp(int a,int b){
return a>b;
}
int a[maxn];
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
fa[i]=i;
}
for(int i=1;i<=m;i++){
int x,y;scanf("%d%d",&x,&y);
fa[fi(x)]=fi(y);
}
for(int i=1;i<=n;i++){
E[fi(i)].push_back(a[i]);
}
for(int i=1;i<=n;i++){
sort(E[i].begin(),E[i].end(),cmp);
}
for(int i=1;i<=n;i++){
printf("%d ",E[fi(i)][t[fi(i)]++]);
}
}
Educational Codeforces Round 14 D. Swaps in Permutation 并查集的更多相关文章
- Educational Codeforces Round 14 D. Swaps in Permutation (并查集orDFS)
题目链接:http://codeforces.com/problemset/problem/691/D 给你n个数,各不相同,范围是1到n.然后是m行数a和b,表示下标为a的数和下标为b的数可以交换无 ...
- Educational Codeforces Round 14 D. Swaps in Permutation(并查集)
题目链接:http://codeforces.com/contest/691/problem/D 题意: 题目给出一段序列,和m条关系,你可以无限次互相交换这m条关系 ,问这条序列字典序最大可以为多少 ...
- Educational Codeforces Round 14 D. Swaps in Permutation
题目链接 分析:一些边把各个节点连接成了一颗颗树.因为每棵树上的边可以走任意次,所以不难想出要字典序最大,就是每棵树中数字大的放在树中节点编号比较小的位置. 我用了极为暴力的方法,先dfs每棵树,再用 ...
- Codeforces Round #582 (Div. 3)-G. Path Queries-并查集
Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...
- Educational Codeforces Round 14
A - Fashion in Berland 水 // #pragma comment(linker, "/STACK:102c000000,102c000000") #inclu ...
- Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)
Codeforces Round #346 (Div. 2)---E. New Reform E. New Reform time limit per test 1 second memory lim ...
- Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径
C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...
- Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)
D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: = 的情况我们用并查集把他们扔到一个集合,然后根据 > ...
- Codeforces Round #376 (Div. 2) C. Socks —— 并查集 + 贪心
题目链接:http://codeforces.com/contest/731/problem/C 题解: 1.看题目时,大概知道,不同的袜子会因为要在同一天穿而差生了关联(或者叫相互制约), 其中一条 ...
随机推荐
- ngx_lua_API 指令详解(二)ngx.re.match/find/gmatch/sub/gsub指令集合
1.先来个官方的ngx.re.match location /ngx_re_match { default_type text/html; content_by_lua_block { local m ...
- 论参数self
此篇文章仅适用于py3.在py2中,a.fuc(x)中的参数x必须是 类a的实例对象,而py3则可以是任意对象.参考绑定方法和非绑定方法 当一个对象添加了一个方法,并且此方法的第一个参数为self,或 ...
- (A - 整数划分 HYSBZ - 1263)(数组模拟大数乘法)
题目链接:https://cn.vjudge.net/problem/HYSBZ-1263 题目大意:中文题目 具体思路:先进了能的拆成3,如果当前剩下的是4,就先不减去3,直接乘4,如果还剩2的话, ...
- F - A计划
题目链接: https://cn.vjudge.net/contest/254150#problem/F wa代码: #include<iostream> #include<stri ...
- fish(自动推荐命令;语法高亮等)
Fish 是 Linux/Unix/Mac OS 的一个命令行 shell,有一些很好用的功能. 自动推荐 VGA 颜色 完美的脚本支持 基于网页的配置 帮助文档自动补全 语法高亮 以及更多 自动推荐 ...
- Socket心跳包机制【转】
转自:https://blog.csdn.net/xuyuefei1988/article/details/8279812 心跳包的发送,通常有两种技术 方法1:应用层自己实现的心跳包 由应用程序自己 ...
- LINUX下 USB转串口 【转】
转自:http://blog.163.com/smilexiao_11015461/blog/static/2122052182012102410399459/ 1.将设备u口插入pc2.输入#lsm ...
- 002_docker构建zookeeper环境
最近因为要维护公司zk环境,所以自己先得搞一套先玩玩 git地址=>https://github.com/jplock/docker-zookeeper/tree/v3.4.9 一.build ...
- 乐视max2 在开发中无法打印某些logcat 解决方案
乐视屏蔽了打印log.d等类型logcat.解决方案:拨号键盘 *#*#76937#*#* 出现页面后选最下面那个选项就有了.
- testng执行用例失败,再次执行
我们通过重写testng的retry方法和transform方法来实现用例失败重跑的功能. 首先添加两个文件 TestngRetry.java public class TestngRetry imp ...