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.看题目时,大概知道,不同的袜子会因为要在同一天穿而差生了关联(或者叫相互制约), 其中一条 ...
随机推荐
- 微信小程序开发 [07] 写在后面的话
写在后面的话基本算是吐槽了,在学完小程序的课程之后,我用博客园的api,写了个闪存的小程序,本来兴致勃勃甚至这篇是准备写"我的第一个小程序发布啦",然而并没有. 不是说我偷懒了没写 ...
- DRUPAL7 : 安装中文版本时遇到的问题
http://yeenav.com是基于Drupal 7+汉化资源 搭建. 期间遇到一些麻烦, 做个记录. 首先把语言包drupal-7.0.zh-hans.po 放在htdocs/drupal-7. ...
- webSphere-Eclipse中配置was的远程调试
目前我们项目中使用的应用服务器多是WebSphere,一直苦于无法进行调试,今天在网上看到一篇,原文是 http://www.cnblogs.com/newstar/archive/2010/04/1 ...
- android studio更新gradle失败的解决办法-转
android studio中每次自动更新gradle时速度实在太慢因为gradle服务器比较慢,所以更新gradle会比较慢,建议先下载下来,然后手动添加到gradle的下载目录,提升速度. 使用下 ...
- POJ 3041&&3020
两道二分图的练手题. 3041:题意大概是在一个N*N的图上有K个东西,你每次可以清除一行或一列上的所有东西.让你求最少的操作次数. 我们根据题意建图.对于每一个点的坐标(x,y)之间连一条边.比如样 ...
- python 回溯法 子集树模板 系列 —— 5、取物搭配问题
问题 有5件不同的上衣,3条不同的裤子,4顶不同的帽子,从中取出一顶帽子.一件上衣和一条裤子作为一种搭配,问有多少种不同的搭配? 分析 换个角度看,现有头.身.腿三个元素,每个元素都有各自的几种状态. ...
- 德哥的PostgreSQL私房菜 - 史上最屌PG资料合集
德哥的PostgreSQL私房菜 - 史上最屌PG资料合集
- stl源码剖析 详细学习笔记 算法总览
//****************************基本算法***************************** /* stl算法总览,不在stl标准规格的sgi专属算法,都以 *加以标 ...
- 创建并使用maven archetype的随笔
maven骨架archetype的意义在于一些项目的基础项:如引入的maven组件,例如eureka,ribben等,不希望每次新建项目都重复做一遍,还有例如公司规范的log格式,单元测试工具等,在新 ...
- Selenium和TestNG
本文档由Felipe Knorr Kuhn撰写,并根据其博客上发布的一系列文章进行改编. 建模您的测试用例 在编写测试用例之前,您需要知道如何验证以及将要验证的内容.让我们使用WordPress “创 ...