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的方法解(略麻烦),形成一个环路的就在一个数组中...
//并查集
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + ;
vector <int> ans[N];
vector <int> root;
int pos[N] , par[N] , a[N] , vis[N]; int Find(int n) {
if(n == par[n])
return n;
return par[n] = Find(par[n]);
} int main()
{
int n , m , u , v;
scanf("%d %d" , &n , &m);
for(int i = ; i <= n ; ++i) {
scanf("%d" , a + i);
par[i] = i;
}
while(m--) {
scanf("%d %d" , &u , &v);
u = Find(u) , v = Find(v);
if(u != v)
par[u] = v;
}
for(int i = ; i <= n ; ++i) {
int u = Find(i);
ans[u].push_back(a[i]);
if(!vis[u]) {
root.push_back(u);
vis[u] = true;
}
}
for(int i = ; i < root.size() ; ++i)
sort(ans[root[i]].rbegin() , ans[root[i]].rend());
for(int i = ; i < n ; ++i)
printf("%d " , ans[Find(i)][pos[Find(i)]++]);
printf("%d\n" , ans[Find(n)][pos[Find(n)]]);
return ;
}
//dfs
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + ;
struct Edge {
int next , to;
}edge[N << ];
vector <int> ans[N];
vector <int> Root;
int a[N] , head[N] , cnt , is[N] , pos[N] , believe[N];
bool vis[N]; inline void add(int u , int v) {
edge[cnt].next = head[u];
edge[cnt].to = v;
head[u] = cnt++;
} void dfs(int root , int u , int par) {
vis[u] = true;
is[u] = root;
ans[root].push_back(a[u]);
for(int i = head[u] ; ~i ; i = edge[i].next) {
int v = edge[i].to;
if(v == par || vis[v])
continue;
dfs(root , v , u);
}
} int main()
{
memset(head , - , sizeof(head));
int n , m , u , v;
scanf("%d %d" , &n , &m);
for(int i = ; i <= n ; ++i)
scanf("%d" , a + i);
while(m--) {
scanf("%d %d" , &u , &v);
add(u , v);
add(v , u);
}
for(int i = ; i <= n ; ++i) {
if(!vis[i]) {
Root.push_back(i);
believe[i] = Root.size() - ;
dfs(i , i , -);
}
}
for(int i = ; i < Root.size() ; ++i) {
sort(ans[Root[i]].rbegin() , ans[Root[i]].rend());
}
for(int i = ; i <= n ; ++i) {
printf("%d" , ans[ Root[believe[is[i]]] ][ pos[believe[is[i]]]++ ]);
if(i != n)
putchar(' ');
else
putchar('\n');
}
return ;
}
Educational Codeforces Round 14 D. Swaps in Permutation (并查集orDFS)的更多相关文章
- 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(并查集)
题目链接:http://codeforces.com/contest/691/problem/D 题意: 题目给出一段序列,和m条关系,你可以无限次互相交换这m条关系 ,问这条序列字典序最大可以为多少 ...
- 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.看题目时,大概知道,不同的袜子会因为要在同一天穿而差生了关联(或者叫相互制约), 其中一条 ...
随机推荐
- 制作SM2证书
前段时间将系统的RSA算法全部升级为SM2国密算法,密码机和UKey硬件设备大都同时支持RSA和SM2算法,只是应用系统的加解密签名验证需要修改,这个更改底层调用的加密动态库来,原来RSA用的对称加密 ...
- 最大流 Dinic + Sap 模板
不说别的,直接上模板. Dinic+当前弧优化: struct Edge{ int x,y,c,ne; }e[M*]; int be[N],all; int d[N],q[N]; int stack[ ...
- [转] 搜索之双向BFS
转自:http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx 如果目标也已知的话,用双向BFS能很大程度上提高速度. 单向时,是 b^le ...
- 最简单的视音频播放示例3:Direct3D播放YUV,RGB(通过Surface)
上一篇文章记录了GDI播放视频的技术.打算接下来写两篇文章记录Direct3D(简称D3D)播放视频的技术.Direct3D应该Windows下最常用的播放视频的技术.实际上视频播放只是Direct3 ...
- 【转】pdf 中如何把几页缩小成一页打印
我用的是Foxit PDF Reader,可以这样设置:文件-打印-打印处理下的页面排列选择“在每张纸上放置多页”-选择每页版数即可. 如果你用的是Adobe Reader,也可以自己找一下,看是否有 ...
- jsoup使用选择器语法来查找元素
问题 你想使用类似于CSS或jQuery的语法来查找和操作元素. 方法 可以使用Element.select(String selector) 和 Elements.select(String sel ...
- Chrome 实用调试技巧
Chrome 实用调试技巧 2016-07-23 如今Chrome浏览器无疑是最受前端青睐的工具,原因除了界面简洁.大量的应用插件,良好的代码规范支持.强大的V8解释器之外,还因为Chrome开发者工 ...
- JavaScript 现状:方言篇
导读 JavaScript 和其他编程语言有一个很大的不同,它不像单纯的一个语言,而像一个由众多方言组成大家族.从 2009 年 CoffeeScript 出现开始,近几年出现了大量基于 JavaSc ...
- [Papers]NSE, $u_3$, Lebesgue space [Kukavica-Ziane, Nonlinearity, 2006]
$$\bex u_3\in L^p(0,T;L^q(\bbR^3)),\quad \frac{2}{p}+\frac{3}{q}=\frac{5}{8},\quad \frac{24}{5}<q ...
- CAKeyframeAnimation
之所以叫做关键帧动画是因为,这个类可以实现,某一属性按照一串的数值进行动画,就好像制作动画的时候一帧一帧的制作一样. 一般使用的时候 首先通过 animationWithKeyPath 方法 创建一 ...