Educational Codeforces Round 14 D. Swaps in Permutation 并查集
2016-08-01 15:16
471 查看
D. Swaps in Permutation
题目连接:
http://www.codeforces.com/contest/691/problem/DDescription
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 61 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 3Hint
题意
给你一个排列,和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
- Educational Codeforces Round 14 D. Swaps in Permutation (并查集orDFS)
- Educational Codeforces Round 14 - D. Swaps in Permutation (优先队列+并差集)
- Educational Codeforces Round 14 A. Fashion in Berland 水题
- Educational Codeforces Round 1 D. Igor In the Museum bfs 并查集
- Educational Codeforces Round 14 A. Fashion in Berland
- Educational Codeforces Round 14 D 并查集
- Codeforces Problem 691D - Swaps in Permutation(并查集+排序)
- Educational Codeforces Round 6 C. Pearls in a Row(贪心)
- 【Educational Codeforces Round 1D】【DFS 联通块打标记法】Igor In the Museum 联通块内墙的面数
- Educational Codeforces Round 7 E. Ants in Leaves 贪心
- Educational Codeforces Round 14 C 好东西啊!!!
- Educational Codeforces Round 31 E. Binary Matrix 并查集
- 【Educational Codeforces Round 33】 C. Rumor (并查集)
- Educational Codeforces Round 6-C. Pearls in a Row(贪心)
- Educational Codeforces Round 1 D. Igor In the Museum
- Educational Codeforces Round 33 (Rated for Div. 2)【C】【并查集】
- Educational Codeforces Round 5 C. The Labyrinth BFS,并查集
- (DFS)Educational Codeforces Round 28 E. Chemistry in Berland