题目链接: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. Mac下安装HBase及详解

    Mac下安装HBase及详解 1. 千篇一律的HBase简介 HBase是Hadoop的数据库, 而Hive数据库的管理工具, HBase具有分布式, 可扩展及面向列存储的特点(基于谷歌BigTabl ...

  2. linux/unix网络编程之 select

    转自http://www.cnblogs.com/zhuwbox/p/4221934.html linux 下的 select 知识点 unp 的第六章已经描述的很清楚,我们这里简单的说下 selec ...

  3. jquery ajax请求 清除缓存

    使用jquery里load方法或者ajax调用页面的时候会存在cache的问题,清除cache的方法: 调用jQuery.ajaxSetup ({cache:false}) 方法即可.

  4. HDU 1677

    Nested Dolls Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  5. 函数buf_pool_init_instance

    buff_pool_t 结构体 详见 /********************************************************************//** Initial ...

  6. 函数fsp_alloc_from_free_frag

    /**********************************************************************//** Allocates a single free ...

  7. sql server 数据库 ' ' 附近有语法错误

    昨天做项目时候,遇到标题的问题,代码跟踪把sql 语句 复制出来在数据库执行不了, 然后重新写个一模一样的,然后在 赋值到代码中,还是同样的错误, 就是不知道哪里出现了错误,最后 把 sql 语句写成 ...

  8. bzoj1934: [Shoi2007]Vote 善意的投票

    最大流..建图方式都是玄学啊.. //Dinic是O(n2m)的. #include<cstdio> #include<cstring> #include<cctype& ...

  9. [反汇编练习] 160个CrackMe之017

    [反汇编练习] 160个CrackMe之017. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  10. Java基础——I/O

    文本I/O与二进制I/O 在计算机中所有的文件都是以二进制的形式来存储的,所以本质上所有的文件都是二进制文件. 文本I/O建立在二进制I/O的基础之上,它能提供字符层次的编码和解码的抽象,在写入一个字 ...