Codeforces 691D Swaps in Permutation【并查集+优先队列+贪心】

186 篇文章 0 订阅
87 篇文章 0 订阅

D. Swaps in Permutation
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Output

Print the only line with n distinct integers p'i (1 ≤ p'i ≤ n) — the lexicographically maximal permutation one can get.

Example
Input
9 6
1 2 3 4 5 6 7 8 9
1 4
4 7
2 5
5 8
3 6
6 9
Output
7 8 9 4 5 6 1 2 3

题目大意:

给你N个数,M个操作,让你给这M个操作规定一个使用顺序,使得得到的最终的序列字典序最大。

每次操作是选择某一次操作,使得两个位子上的数字进行交换。


思路:


1、考虑问题有一个递推性质,假设现在有两个操作:

x y

x z

那么显然,位子x上的数最终可以在y上也可以在z上,同理,y也可以在x上,也可以在z上,再同理,z可以在x上也可以在y上。

那么其实我们规定其操作顺序,将一个操作视为一个无向边,那么对于一个联通块中的几个位子上的数,就可以互相穿插着安排了。


2、也就是说,将一个操作看成一个无向边,对于一个联通块中的所有元素,都可以任意安排位子。

那么我们维护1e6个优先队列,s【i】中存入的元素,是表示以i为根的联通块在原序列中的位子。那么优先级我们可以定义为从大到小。

接下来我们再将元素按照值从小到大排序,那么我们每一个小的值,都从队头拿出一个最大的可以放置的位子,将这个值放置在那个位子上即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
struct node
{
    int pos,val,root;
}b[1000500];
 priority_queue<int,vector<int>,less<int> >s[1000500];
int a[1000500];
int f[1000500];
int ans[1000500];
int find(int a)
{
    int r=a;
    while(f[r]!=r)
    r=f[r];
    int i=a;
    int j;
    while(i!=r)
    {
        j=f[i];
        f[i]=r;
        i=j;
    }
    return r;
}
void merge(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
    {
        f[B]=A;
    }
}
int cmp(node a,node b)
{
    if(a.root==b.root)
    {
        return a.val<b.val;
    }
    else return a.root<b.root;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(ans,-1,sizeof(ans));
        for(int i=1;i<=n;i++)f[i]=i;
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            merge(x,y);
        }
        int cnt=0;
        for(int i=1;i<=n;i++)
        {
            find(i);
            b[i].root=f[i];
            b[i].pos=i;
            b[i].val=a[i];
            s[f[i]].push(i);
        }
        sort(b+1,b+1+n,cmp);
        for(int i=1;i<=n;i++)
        {
            int rrr=b[i].root;
            int anspos=s[rrr].top();
            s[rrr].pop();
            ans[anspos]=b[i].val;
        }
        for(int i=1;i<=n;i++)
        {
            printf("%d ",ans[i]);
        }
        printf("\n");
    }
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值