这个题刚开始我以为是每个交换只能用一次,然后一共m次操作

结果这个题的意思是操作数目不限,每个交换也可以无限次

所以可以交换的两个位置连边,只要两个位置连通,就可以呼唤

然后连通块内排序就好了

#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long LL;
const int N=1e6+;
const LL mod=1e9+;
struct Edge{
int v,next;
}edge[N<<];
int head[N],tot,a[N],n,m,cnt;
vector<int>bcc[N],ran[N];
void add(int u,int v){
edge[tot].v=v;
edge[tot].next=head[u];
head[u]=tot++;
}
bool vis[N];
void dfs(int u){
vis[u]=true;
bcc[cnt].push_back(a[u]);
ran[cnt].push_back(u);
for(int i=head[u];~i;i=edge[i].next)
if(!vis[edge[i].v])dfs(edge[i].v);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;++i)
scanf("%d",&a[i]);
memset(head,-,sizeof(head));
for(int i=;i<=m;++i){
int u,v;scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
for(int i=;i<=n;++i){
if(vis[i])continue;
++cnt;dfs(i);
}
for(int i=;i<=cnt;++i){
sort(bcc[i].begin(),bcc[i].end());
sort(ran[i].begin(),ran[i].end());
int sz=bcc[i].size()-;
for(int j=,k=sz;j<=sz;++j,--k)
a[ran[i][j]]=bcc[i][k];
}
for(int i=;i<n;++i)printf("%d ",a[i]);
printf("%d\n",a[n]);
return ;
}

codeforces 691D Swaps in Permutation DFS的更多相关文章

  1. Codeforces 691D Swaps in Permutation

    Time Limit:5000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Prac ...

  2. 【搜索】【并查集】Codeforces 691D Swaps in Permutation

    题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...

  3. codeforces 691D D. Swaps in Permutation(dfs)

    题目链接: D. Swaps in Permutation time limit per test 5 seconds memory limit per test 256 megabytes inpu ...

  4. CodeForces 691D:Swaps in Permutation(并查集)

    http://codeforces.com/contest/691/problem/D D. Swaps in Permutation   You are given a permutation of ...

  5. Educational Codeforces Round 14 D. Swaps in Permutation 并查集

    D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are gi ...

  6. Swaps in Permutation

    Swaps in Permutation You are given a permutation of the numbers 1, 2, ..., n and m pairs of position ...

  7. codeforces 615 B. Longtail Hedgehog (DFS + 剪枝)

    题目链接: codeforces 615 B. Longtail Hedgehog (DFS + 剪枝) 题目描述: 给定n个点m条无向边的图,设一条节点递增的链末尾节点为u,链上点的个数为P,则该链 ...

  8. codeforces 691D(数据结构)

    D. Swaps in Permutation time limit per test 5 seconds memory limit per test 256 megabytes input stan ...

  9. Educational Codeforces Round 14 D. Swaps in Permutation (并查集orDFS)

    题目链接:http://codeforces.com/problemset/problem/691/D 给你n个数,各不相同,范围是1到n.然后是m行数a和b,表示下标为a的数和下标为b的数可以交换无 ...

随机推荐

  1. POJ3414Pots

    http://poj.org/problem?id=3414 题意 : 大意是说给你两个杯子的体积和一个目标体积,a,b,c,通过对a,b进行6种操作,调出c体积的水,6种操作分别是把a倒满,把b倒满 ...

  2. JDK与JRE

    dos命令行中常见的命令: 1.dir:列出当前目录下的文件以及文件夹 2.md:创建目录(即文件夹) |-----C:\>md kkk(在C盘下创建了一个名为kkk的文件夹) 3.rd:删除目 ...

  3. Mysql 1030 Got error -1 from storage engine 错误解决

    检查你的my.cnf或my.ini,里面会有一个参数innodb_force_recovery,你看看他的值,默认是没有这个参数,没有的话,他的默认值是0,这个参数的值如果大于0,innodb会被禁止 ...

  4. 解决Cygwin中文乱码

    如下图所示,在执行ping或者cmd.exe命令时,Cygwin出现中文乱码: 解决方案: 在Cygwin终端上右键-->Options…-->Text-->修改Locale 为 z ...

  5. C# 时间函数

    DateTime dt = DateTime.Now; string str = dt.ToString("yyyy-MM-dd");//2013-09-07 str = dt.T ...

  6. Tomcat集群配置学习篇-----分布式应用

    Tomcat集群配置学习篇-----分布式应用 现目前基于javaWeb开发的应用系统已经比比皆是,尤其是电子商务网站,要想网站发展壮大,那么必然就得能够承受住庞大的网站访问量:大家知道如果服务器访问 ...

  7. 区分int a() 和 int a

    事因 #include <iostream> using namespace std; struct A { A(int) {} A() {} void fun() {}; }; int ...

  8. 什么是HttpOnly

    1.什么是HttpOnly? 如果您在cookie中设置了HttpOnly属性,那么通过js脚本将无法读取到cookie信息,这样能有效的防止XSS攻击,具体一点的介绍请google进行搜索 2.ja ...

  9. [Codeforces677B]Vanya and Food Processor(模拟,数学)

    题目链接:http://codeforces.com/contest/677/problem/B 题意:n个土豆,每个土豆高ai.现在有个加工机,最高能放h,每次能加工k.问需要多少次才能把土豆全加工 ...

  10. IE css expression(表达式)

    很多时候我们需要对IE6的bug写一些hack,如max-height,absolute元素高度100%等. css里面的 expression(表达式)和js里面的差不多,如: 获取当前元素的高度: ...