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.看题目时,大概知道,不同的袜子会因为要在同一天穿而差生了关联(或者叫相互制约), 其中一条 ...
随机推荐
- Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) F 构造
http://codeforces.com/contest/967/problem/F 题目大意: 有n个点,n*(n-1)/2条边的无向图,其中有m条路目前开启(即能走),剩下的都是关闭状态 定义: ...
- Git与GitHub学习笔记(二)提交的一些笔记
1.合并分支的使用一定要切换到master分支上去合并:git merge company2.切换分支的时候一定要提交干净本地分支的代码,才可以切换分支,否则提示错误信息: 3.这时候我们做的就是提交 ...
- Docker CE的安装 与镜像加速
Docker CE 的安装与镜像加速 Docker CE是docker的开源版本 CENTOS 安装Docker CE 系统要求: 操作系统需要使用centos7() centos-extras库 必 ...
- Java入门系列(八)多线程
基本线程类指的是Thread类,Runnable接口,Callable接口 典型多线程问题 生产者-消费者 死锁问题
- [转载]win7休眠后网络断开怎么办?如何设置?
http://jingyan.baidu.com/article/8065f87fc87d0423312498af.html 有时会遇到在Windows7系统休眠模式下会自动断开网络连接,唤醒系统也是 ...
- bzoj 1432 数学(找规律)
我们可以发现所有的情况(除n=1时),都可以找到两个交叉的直线,就是第一层的那 两个线段所在的直线如图中左 那么我们以这个为准,两边对称着加直线,会得到右图,每一层是折线,且每 加一对儿就多两条线段, ...
- windows中连接hive-客户端
参考连接:http://lxw1234.com/archives/2016/09/723.htm 这里主要介绍:Oracle SQL Developer 1. 下载,免安装包 SQL Develope ...
- POI操作Excel详解,HSSF和XSSF两种方式
package com.tools.poi.lesson1; import java.io.FileInputStream; import java.io.FileNotFoundException; ...
- HDU 2200 Eddy's AC难题
解析: 1.可以从中任选m个人(n=>m>=2),有Cn(m)中选择; 2.再把这m个人分2组(每个人都要分组),要使满足最小ac数大于最大ac数,只需要在m个人中插板即可: 例如: m个 ...
- .NetCore 中使用AppMetrics向InfluxDB中添加监控数据并通过Grafana图像分析
考虑到分布式部署监控环境是所有的请求情况,所以这一块一般在网关GateWay里面加比较省事,聚合在一起的,如果放在api服务中,如果只有1个还好,一旦部署Node多是很痛苦的事情 这天需要添加的Nug ...