这个题刚开始我以为是每个交换只能用一次,然后一共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. httpclient发送multipart/form-data类型参数和用MultipartRequest接收参数

    一.利用HttpClient发送基于Content-Type="multipart/form-data"形式的表单 package com.test.httpclient; imp ...

  2. 简单的线程同步问题:两个线程交替执行N次【Synchronized、Lock、ArrayBlockingQueue】

    方法一:传统的线程方法import org.apache.log4j.Logger; /** * 两个线程执行的代码片段要实现同步互斥的效果,它们必须用同一个Lock对象.<br/> * ...

  3. JAVA XMLConfiguration解决StringXML及多节点

    import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configurat ...

  4. 移植 FFMPEG-2.2.4 -(编译)

    源码下载:http://www.ffmpeg.org/download.html编译安装: http://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu su ...

  5. 如何获取多核、多cpu系统中指定cpu的序列号

    如何获取多核.多cpu系统中指定cpu的序列号作者:SkyJacker(转贴请保持完整并注明作者和出处)http://www.cnpack.orgCnPack IV  QQ Group: 130970 ...

  6. SQLite数据插入异常

    对比两条SQL语句 1.insert into MemberInfo(MTypeId,MName,MPhone,MMoney,MIsDelete) values(@tid,@name,@phone,@ ...

  7. QTP数据驱动之读取Excel数据

    这个代码的原理是把Excel的数据当做数据库里的数据一样处理,可以对Excel用select来检索需要的数据,然后把数据以键值对的形式保存到oDict里,方便在用例层来调用 Class oDataDi ...

  8. AsciiDoc Markup Syntax Summary

    AsciiDoc Markup Syntax Summary ============================== A summary of the most commonly used ma ...

  9. javadoc 和 javadoc注释规范

    javadoc是Sun公司提供的一个技术,它从程序源代码中抽取类.方法.成员等注释形成一个和源代码配套的API帮助文档. javadoc命令是用来生成自己API文档的,使用方式:在dos中在目标文件所 ...

  10. NuGet学习笔记

    NuGet学习笔记(1)——初识NuGet及快速安装使用 NuGet学习笔记(2)——使用图形化界面打包自己的类库 NuGet学习笔记(3)——搭建属于自己的NuGet服务器