题目链接:http://codeforces.com/contest/691/problem/D

题意:

题目给出一段序列,和m条关系,你可以无限次互相交换这m条关系 ,问这条序列字典序最大可以为多少

思路:

并查集维护这m条关系,用个vector存一下当前点所能到达的点,拍下序大的优先,输出的时候输出当前点所能找到的最大的数,已输出的标记下就好了。

实现代码:

#include<bits/stdc++.h>
using namespace std;
const int M = 1e6+;
int f[M],vis[M],a[M]; int Find(int x){
if(x==f[x])return x;
return f[x]=Find(f[x]);
} void mix(int x,int y){
int fx = Find(x);
int fy = Find(y);
if(fx != fy) f[fx] = fy;
} bool cmp(int a,int b){
return a > b;
} vector<int>v[M]; int main()
{
int n,x,y,m;
scanf("%d%d",&n,&m);
for(int i = ;i <= n;i ++){
scanf("%d",&a[i]);
f[i] = i;
}
for(int i = ;i <= m;i ++){
scanf("%d%d",&x,&y);
mix(x,y);
}
for(int i = ;i <= n;i ++)
v[Find(i)].push_back(a[i]);
for(int i = ;i <= n;i ++)
sort(v[i].begin(),v[i].end(),cmp);
for(int i = ;i <= n;i ++){
printf("%d ",v[Find(i)][vis[Find(i)]++]);
}
}

Educational Codeforces Round 14 D. Swaps in Permutation(并查集)的更多相关文章

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

    D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are gi ...

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

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

  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. ORACLE SEQUENCE 具体解释

    1.    About Sequences(关于序列) 序列是数据库对象一种. 多个用户能够通过序列生成连续的数字以此来实现主键字段的自己主动.唯一增长,而且一个序列可为多列.多表同一时候使用. 序列 ...

  2. MIPI Alliance (MIPI联盟)

    一.介绍 1.MIPI联盟,即移动产业处理器接口(Mobile Industry Processor Interface 简称MIPI)联盟.MIPI(移动产业处理器接口)是MIPI联盟发起的为移动应 ...

  3. 执行力:Just Do It

    执行力,最最关键的一点就是,立即去做,不要想太多. 当有一件事需要去做的时候,你的大脑肯定是接受到了"某种信号",比如来了一个灵感.受到一点启发.做某件事突然来了兴趣.或者想去探讨 ...

  4. Linux 学习日记 1

    这是我第一次系统地学习Linux,希望通过这个学习日记收获一些东西把-- @_@ Grub - 启动管理器   在启动时让用户选择要启动的系统.(但是windows比较霸道--重装windows后会将 ...

  5. flask之jinjia2模板(二)

    1.1.模板传参 (1)主程序 from flask import Flask,render_template app = Flask(__name__) @app.route('/') def he ...

  6. Android Studio Xposed模块编写(一)

    1.环境说明 本文主要参考https://my.oschina.net/wisedream/blog/471292?fromerr=rNPFQidG的内容,自己实现了一遍,侵权请告知 已经安装xpos ...

  7. 带WIFI模块布局布线要点。

    带WIFI模块布局布线要求: 1: RF底部不能铺铜要挖空不能有GND否则RF信号会被耦合掉从而无法发送出去. 2:WIFI模块下方不能打孔尽量不走线不打孔避开其他信号穿过下方,要整体的铺铜 3:连接 ...

  8. Python+Selenium+Unittest+Ddt+HTMLReport分布式数据驱动自动化测试框架结构

    1.Business:公共业务模块,如登录模块,可以把登录模块进行封装供调用 ------login_business.py from Page_Object.Common_Page.login_pa ...

  9. C#Dictionary使用记录

    一.区别 在工作中经常遇到C#数组.ArrayList.List.Dictionary存取数据,其区别和优劣势为: 初始化 数组: int[] buff = new int[6]; ArrayList ...

  10. 毕业设计---json,Struts,ajax以及JQuery简单案例

    <!-- Struts2的xml文件配置 --><struts> <package name="default" namespace="/& ...