Codeforces 691D Swaps in Permutation
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, sopk = 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 apositions, not the values to swap.
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
7 8 9 4 5 6 1 2 3
/*
我本来还以为每种操作只能操作一次,结果可以操作无穷次
“At each step you can choose a pair from the given positions and swap the numbers in that positions.”
怪我英语不好,┗( T﹏T )┛
DFS或并查集求连通块。
把每一条允许交换的位置看做是图中的一条边,画图会发现:一个连通块内的那些位置是可以任意交换的。
因此,只需找出连通块,每一块内的值从大到小排序就行。 */
#include <iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int team[],teampos[],a[];
bool vis[];
int cnt,n,m;
vector<int> s[];
void dfs(int k)
{
team[++cnt]=a[k];
teampos[cnt]=k;
vis[k]=;
for(int i=;i<s[k].size();i++)
if (!vis[s[k][i]]) dfs(s[k][i]);
}
bool cmp(int a,int b)
{
return a>b;
} int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=;i<=n;i++)
{scanf("%d",&a[i]);vis[i]=;s[i].clear();} for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
s[x].push_back(y);
s[y].push_back(x);
}
for(int i=;i<=n;i++)
if (!vis[i])
{
cnt=;
dfs(i);
sort(teampos+,teampos+cnt+);
sort(team+,team+cnt+,cmp);
for(int j=;j<=cnt;j++)
a[teampos[j]]=team[j];
}
for(int i=;i<n;i++)
printf("%d ",a[i]);
printf("%d\n",a[n]);
}
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),每个操作可以交 ...
- codeforces 691D Swaps in Permutation DFS
这个题刚开始我以为是每个交换只能用一次,然后一共m次操作 结果这个题的意思是操作数目不限,每个交换也可以无限次 所以可以交换的两个位置连边,只要两个位置连通,就可以呼唤 然后连通块内排序就好了 #in ...
- CodeForces 691D:Swaps in Permutation(并查集)
http://codeforces.com/contest/691/problem/D D. Swaps in Permutation You are given a permutation of ...
- codeforces 691D D. Swaps in Permutation(dfs)
题目链接: D. Swaps in Permutation time limit per test 5 seconds memory limit per test 256 megabytes inpu ...
- Educational Codeforces Round 14 D. Swaps in Permutation 并查集
D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are gi ...
- Swaps in Permutation
Swaps in Permutation You are given a permutation of the numbers 1, 2, ..., n and m pairs of position ...
- codeforces 691D(数据结构)
D. Swaps in Permutation time limit per test 5 seconds memory limit per test 256 megabytes input stan ...
- 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条关系 ,问这条序列字典序最大可以为多少 ...
随机推荐
- ajax实现下拉列表联动
下拉框代码 <fieldset style="margin-bottom:5px;"> <div class="form-group"> ...
- 一个快速、高效的Levenshtein算法实现——代码实现
在网上看到一篇博客讲解Levenshtein的计算,大部分内容都挺好的,只是在一些细节上不够好,看了很长时间才明白.我对其中的算法描述做了一个简单的修改.原文的链接是:一个快速.高效的Levensht ...
- 负载均衡lvs_dr_tcp_http单调度
准备三台虚拟,均为CentOS6.5 x86_64注意,配置过程中,保持端口的一致性.director (eth0 192.168.1.189, vip eth0:0: 192.168.1.18) D ...
- 防范XSS
.net framework4.5 提供了AntiXss类,来防范XSS攻击. 在开放指令的同时过滤危险字符串,使用AntiXss.GetSafeHtmlFragment(html)方法,具体可以参照 ...
- Learning from the CakePHP source code - Part II
原文:http://debuggable.com/posts/learning-from-the-cakephp-source-code-part-ii:480f4dd6-57fc-4715-8709 ...
- 关于PHP的一小段代码求解如下求解"%2\$s"
<?php$format = "The %2\$s contains %1\$d monkeys";printf($format, 8, "北京");?& ...
- Spring MVC(一)
MVC这种设计模式,不光运用于Web领域,而且也能用于非Web领域,MVC特指一种表现层设计模式,不限于Java语言 spring mvc属于spring框架的后续产品,用在基于MVC的表现层开发,类 ...
- iOS 富文本点击事件
#import "ViewController.h" #define font 17 @interface ViewController ()<UITextViewDeleg ...
- log4j.properties 的使用详解
一.log4j.properties 的使用详解 1.输出级别的种类 ERROR.WARN.INFO.DEBUGERROR 为严重错误 主要是程序的错误WARN 为一般警告,比如session丢失IN ...
- FireFox的配置文件的引用
1.firefox的配置文件的实际路径:C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\oviavula.default ...