题目链接: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)的更多相关文章

  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(并查集)

    题目链接:http://codeforces.com/contest/691/problem/D 题意: 题目给出一段序列,和m条关系,你可以无限次互相交换这m条关系 ,问这条序列字典序最大可以为多少 ...

  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. 一个简单的iBatis入门例子

    一个简单的iBatis入门例子,用ORACLE和Java测试 目录结构: 1.导入iBatis和oracle驱动. 2.创建类Person.java package com.ibeats;import ...

  2. cd /d %~dp0

    cd /d %~dp0 注册服务的bat要用到,普通的执行没关系,但是用“管理员角色”执行,默认的起始目录会到  c:\windows\system 下,用上面一句可以回到当前执行bat的目录

  3. bzoj1312

    忘写题解了,经典的最大密度子图 可以类似分数规划的做,二分密度,然后转化为最大权闭合子图做,判断是否大于0 注意方案的输出 const eps=1e-6; lim=1e-12; inf=; type ...

  4. WebView点击加载的页面中的按钮时不弹出新窗口以及在加载后执行javascript

    mWebView.setWebViewClient(new WebViewClient() { //点击网页中按钮时,在原页面打开 public boolean shouldOverrideUrlLo ...

  5. ActivityManager: Warning: Activity not started, its current task has been brought to the front 的的问题

    运行android程序的时候提示:ActivityManager: Warning: Activity not started, its current task has been brought t ...

  6. I.MX6 git patch

    /********************************************************************** * I.MX6 git patch * 说明: * 之前 ...

  7. 使用D3D渲染YUV视频数据

    源代码下载 在PC机上,对于YUV格式的视频如YV12,YUY2等的显示方法,一般是采用DIRECTDRAW,使用显卡的OVERLAY表面显示.OVERLAY技术主要是为了解决在PC上播放VCD而在显 ...

  8. linux笔试

    在对linux基本知识的归纳总结之后,这里是一份linux的测试题.希望能帮助大家复习和熟悉linux知识. 一.选择题 1.cron 后台常驻程序 (daemon) 用于:  A. 负责文件在网络中 ...

  9. 底部菜单栏(二) TabHost & RadioGroup 实现

    需求:使用TabHost & RadioGroup实现底部菜单栏: 效果图: 实现分析: 1.目录结构: 代码实现: 1. activity_main.xml <?xml version ...

  10. equals(),hashcode()方法详解

    Java中的equals方法和hashCode方法是Object中的,所以每个对象都是有这两个方法的,有时候我们需要实现特定需求,可能要重写这两个方法,今天就来介绍一些这两个方法的作用. equals ...