Swaps in Permutation

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, ..., np 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 pi (1 ≤ pi ≤ n) — the lexicographically maximal permutation one can get.

Example

input

9 61 2 3 4 5 6 7 8 91 44 72 55 83 66 9

output

7 8 9 4 5 6 1 2 3分析:一个联通体内的数从大到小排列就好;代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e6+10;
const int mod=1e6+3;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p%mod;p=p*p%mod;q>>=1;}return f;}
int n,m,a[maxn],vis[maxn];
vi b[maxn],c;
set<int>p,q;
void dfs(int now)
{
    vis[now]=1;q.insert(now);c.pb(a[now]);
    for(int x:b[now])if(!vis[x])dfs(x);
}
int main()
{
    int i,j,k,t;
    scanf("%d%d",&n,&m);
    rep(i,1,n)scanf("%d",&a[i]);
    while(m--)
    {
        scanf("%d%d",&j,&k);b[j].pb(k),b[k].pb(j);
        p.insert(j),p.insert(k);
    }
    for(int x:p)
    {
        if(!vis[x])
        {
            q.clear();c.clear();
            dfs(x);
            sort(c.rbegin(),c.rend());j=0;
            for(int y:q)a[y]=c[j++];
        }
    }
    rep(i,1,n)printf("%d ",a[i]);
    //system ("pause");
    return 0;
}
时间: 2024-10-01 01:31:43

Swaps in Permutation的相关文章

codeforces 691D Swaps in Permutation(并查集)

题意: 给你一个长度为n的数列,然后给你m组数, 表示这两个数可以交换 然后让你给出字典序最大的数列 思路: 用并查集,可交换的数都是成组的,把同一并查集中的数加在根节点的vector后, 在一个并查集中的数,从大到输出就好了 /* *********************************************** Author :devil ************************************************ */ #include <cstdio>

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

题目链接:http://codeforces.com/problemset/problem/691/D 给你n个数,各不相同,范围是1到n.然后是m行数a和b,表示下标为a的数和下标为b的数可以交换无数次.问你最后字典序最大的数列是什么. 将下面的a和b用并查集联系起来存到祖节点对应的数组中,然后从大到小排序数组,最后依次按照父节点的数组中的顺序输出. 也可以用dfs的方法解(略麻烦),形成一个环路的就在一个数组中... 1 //并查集 2 #include <bits/stdc++.h> 3

【搜索】【并查集】Codeforces 691D Swaps in Permutation

题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交换X Y位置上的数字,求可以得到的最大字典序的数列. 题目思路: [搜索][并查集] 这题可以用搜索或者并查集写,都能过. 把位置分成若干块,每一块里面的位置都是可以被这一块里另一个位置经过若干次调换的(类似强连通,位置可达). 然后把每一块位置里的 位置按从小到大排序,位置上的值按从大到小排序,依次填入位置

Codeforces 691D. Swaps in Permutation

题目链接:http://codeforces.com/problemset/problem/691/D 题意: 给你一个含有 N 个不重复数的序列, 以及 M 对形如 (ai, bj) 的数, (ai ,bj) 代表着可以把第 ai 个数可以和第 bj 个数进行交换, 问如何交换才能使得变换后的字典序最大,每个变化可选可不选,也可选多次. 思路: 可以想到, 如果 a 可以和 b 换, b 可以和 c 换, 那么 a 就可以间接的和 c 换, 依次类推, 如果某些位置直接可以变换, 那么与这些位

CF 500 B. New Year Permutation 并查集

User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible. Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k

#404 (div2)Anton and Permutation (分块处理)

Anton likes permutations, especially he likes to permute their elements. Note that a permutation of n elements is a sequence of numbers {a1,?a2,?...,?an}, in which every number from 1 to n appears exactly once. One day Anton got a new permutation and

CF785CAnton and Permutation(分块 动态逆序对)

Anton likes permutations, especially he likes to permute their elements. Note that a permutation of n elements is a sequence of numbers {a1, a2, ..., an}, in which every number from 1 to n appears exactly once. One day Anton got a new permutation and

counting swaps

3602 Counting Swaps 0x30「数学知识」例题 背景 https://ipsc.ksp.sk/2016/real/problems/c.html Just like yesterday (in problem U of the practice session), Bob is busy, so Alice keeps on playing some single-player games and puzzles. In her newest puzzle she has a

31. Next Permutation

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replaceme