Educational Codeforces Round 14 D. Swaps in Permutation(并查集)
题目链接: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(并查集)的更多相关文章
- Educational Codeforces Round 14 D. Swaps in Permutation 并查集
D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are gi ...
- 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
题目链接 分析:一些边把各个节点连接成了一颗颗树.因为每棵树上的边可以走任意次,所以不难想出要字典序最大,就是每棵树中数字大的放在树中节点编号比较小的位置. 我用了极为暴力的方法,先dfs每棵树,再用 ...
- Codeforces Round #582 (Div. 3)-G. Path Queries-并查集
Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...
- Educational Codeforces Round 14
A - Fashion in Berland 水 // #pragma comment(linker, "/STACK:102c000000,102c000000") #inclu ...
- 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 ...
- Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径
C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...
- Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)
D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: = 的情况我们用并查集把他们扔到一个集合,然后根据 > ...
- Codeforces Round #376 (Div. 2) C. Socks —— 并查集 + 贪心
题目链接:http://codeforces.com/contest/731/problem/C 题解: 1.看题目时,大概知道,不同的袜子会因为要在同一天穿而差生了关联(或者叫相互制约), 其中一条 ...
随机推荐
- overflow的使用
<div id="topFieldDiv" style="width: 650px; height: 150px; overflow-y: hidden" ...
- Debian 鼠标左右手
环境:debian testing;xfce4桌面 在debian中想把鼠标改为左手操作,在设置中调整鼠标的按钮为左撇子根本没用!网上搜索后发现事实很简单,简单到不知该怎么说. 废话少说,放码过来. ...
- 简单直白的去理解AOP,了解Spring AOP,使用 @AspectJ - 读书笔记
AOP = Aspect Oriental Programing 面向切面编程 文章里不讲AOP术语,什么连接点.切点.切面什么的,这玩意太绕,记不住也罢.旨在以简单.直白的方式理解AOP,理解Sp ...
- 翻译 | The Principles of OOD 面向对象设计原则
本文首发于vivo互联网技术微信公众号 https://mp.weixin.qq.com/s/Q_pziBUhKRywafKeY2T7YQ 作者:Robert C. Martin 翻译:张硕 本文由来 ...
- Tomcat端口被占用解决方案
Tomcat端口被占用解决方法 1.在dos下,输入 netstat -ano|findstr 8080 //说明:查看占用8080端口的进程,显示占用端口的进程 2.taskkill /pid 19 ...
- Linux中tty、pty、pts的概念区别 转载
基本概念: > tty(终端设备的统称): tty一词源于Teletypes,或teletypewriters,原来指的是电传打字机,是通过串行线用打印机键盘通过阅读和发送信息的东西,后来这东西 ...
- 继承类中static数据值
class A{ static int num = 1; public static void Display(){ System.out.println( num ); } } class B ex ...
- jersey2 整合 spring + hibernate + log4j2
整合 spring jersey2 官方还未正式支持 spring4, 但网上有好多支持方案,折腾了一圈后,还是用了 spring3; pom 添加以下依赖配置 <!-- Spring --&g ...
- grunt-inline:一个资源内嵌插件
一.插件简介 将引用的外部资源,如js.css.img等,内嵌到引用它们的文件里去. 二.使用场景 在项目中,出于某些原因,有的时候我们需要将一些资源,比如js脚本内嵌到页面中去.比如我们的html页 ...
- VirtualBox虚拟机怎么导入已经存在的vdi文件
VirtualBox虚拟机怎么导入已经存在的vdi文件 第一章 1.原因 早上一不小心将virtualBox 卸载了,(不知道怎么了, 里面得虚拟机全部都没有了,但是vdi文件还在) 2.解决办法 直 ...