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.看题目时,大概知道,不同的袜子会因为要在同一天穿而差生了关联(或者叫相互制约), 其中一条 ...
随机推荐
- OpenGL笔记(三) GLSL语法与内建函数
GLSL,OpenGL Shading Language,GLSL中没有指针,并且没有任何类型的字符串或字符. (1)GLSL的修饰符与基本数据类型 const:用于声明非可写的编译时常量变量: at ...
- 使用Git进行协同开发
用了一段时间github,一直想用时间来对git的使用来做一段笔记,前段时间比较忙,现在沉下心来学习也是极好的. 很多项目开发会采用git这一优秀的分布式版本管理工具来进行项目版本管理.因为git的使 ...
- odoo明细表汇总数据
一.在主表中#改动地方 总结算金额 求和:def _get_subtotal2(self, cr, uid, ids, field_name, arg, context=None): # 初始化 re ...
- python_basic
开始学习python ,欢迎一起进步.
- python3 的 round 函数的 练习
python3 的 round 函数感觉很别扭,其运算结果与习惯不相符.特记录下来: 代码 ''' python 3的 round 函数 是"四舍六入五成双"的 https://w ...
- 【HNOI2016】网络
题面 题解 考虑整体二分. 定义整体二分函数solve(l, r, ql, qr)表示操作权值在\([l, r]\)中,对\([ql, qr]\)的询问进行二分. 这样的话check就会很简单,先按照 ...
- P2463 [SDOI2008]Sandy的卡片
写一种\(O(nm)\)的做法,也就是\(O(\sum 串长)\)的. 先通过差分转化,把每个数变成这个数与上一个数的差,第一个数去掉,答案就是最长公共子串+1 按照套路把所有串拼起来,中间加一个分隔 ...
- html点击链接打开新窗口
html标记中格式为<a href="url"> text </a> 此时,内容在原来窗口呈现,如果想新开窗口,可以采用下列方式. 1. <a hre ...
- 微信小程序之用户信息授权 wx.getUserInfo
用户授权 <button open-type="getUserInfo" bindgetuserinfo='getUser'>授权用户信息</button> ...
- mvc5.0-路由
:first-child{margin-top:0!important}.markdown-body>:last-child{margin-bottom:0!important}.markdow ...