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 并查集的更多相关文章

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

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

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

    题目链接:http://codeforces.com/contest/691/problem/D 题意: 题目给出一段序列,和m条关系,你可以无限次互相交换这m条关系 ,问这条序列字典序最大可以为多少 ...

  3. Educational Codeforces Round 14 D. Swaps in Permutation

    题目链接 分析:一些边把各个节点连接成了一颗颗树.因为每棵树上的边可以走任意次,所以不难想出要字典序最大,就是每棵树中数字大的放在树中节点编号比较小的位置. 我用了极为暴力的方法,先dfs每棵树,再用 ...

  4. Codeforces Round #582 (Div. 3)-G. Path Queries-并查集

    Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...

  5. Educational Codeforces Round 14

    A - Fashion in Berland 水 // #pragma comment(linker, "/STACK:102c000000,102c000000") #inclu ...

  6. 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 ...

  7. Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径

    C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...

  8. Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)

    D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: =  的情况我们用并查集把他们扔到一个集合,然后根据 > ...

  9. Codeforces Round #376 (Div. 2) C. Socks —— 并查集 + 贪心

    题目链接:http://codeforces.com/contest/731/problem/C 题解: 1.看题目时,大概知道,不同的袜子会因为要在同一天穿而差生了关联(或者叫相互制约), 其中一条 ...

随机推荐

  1. 何凯文每日一句打开||DAY8

  2. Python核心编程——Chapter16

    好吧,在拜读完<Python网络编程基础>之后,回头再搞一搞16章的网络编程吧. Let‘s go! 16.4.修改书上示例的TCP和UDP客户端,使得服务器的名字不要在代码里写死,要允许 ...

  3. OC中实现锁

    熟悉多线程开发的同学对锁肯定不陌生,但是OC中如何实现锁呢?给大家科普一下. 首先构建一个测试用的类,假想它是我们的一个共享资源,method1与method2是互斥的,代码如下: @implemen ...

  4. python中的 __repr__和__str__

    __repr__,被内置函数repr用于把一个对象用"官方"的字符串形式表示出来(终端友好)    1.值传给eval()来返回一个对象的字符串表示形式    2.否则返回一个尖括 ...

  5. 在一台win10上启动多个mysql

    1.因为项目需要用一个已经有数据的mysql,而我之前已经安装了一个mysql(之前的mysql上面也是有东西,不想删除)  想办法.... mysqld.exe --defaults-file=D: ...

  6. 利用Python生成随机密码

    #coding:utf-8 #利用python生成一个随机10位的字符串 import string import random import re list = list(string.lowerc ...

  7. Android WebView 详解

    相关API 相关类介绍 WebResourceRequest 添加于API21,封装了一个Web资源的请求信息,包含:请求地址,请求方法,请求头,是否主框架,是否用户点击,是否重定向 WebResou ...

  8. 虚拟机Failed to start LSB: Bring up/down networking

      1.执行 service network restart 出现以下错误 Restarting network (via systemctl):  Job for network.service f ...

  9. LeetCode(17):电话号码的字母组合

    Medium! 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23& ...

  10. Windows xp下安装sql server2005所碰到的一些问题及解决方法

    之前提到的帮老板做的一个中船重工的项目,其中的一个子模块:windows下获取特定进程网络流量 一开始是用VS2010做的,后来老板把项目书拿给我看后,明确要求开发环境为VS2005和Sql serv ...