codeforces 691D Swaps in Permutation DFS
这个题刚开始我以为是每个交换只能用一次,然后一共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的更多相关文章
- Codeforces 691D Swaps in Permutation
Time Limit:5000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Status Prac ...
- 【搜索】【并查集】Codeforces 691D Swaps in Permutation
题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...
- codeforces 691D D. Swaps in Permutation(dfs)
题目链接: D. Swaps in Permutation time limit per test 5 seconds memory limit per test 256 megabytes inpu ...
- CodeForces 691D:Swaps in Permutation(并查集)
http://codeforces.com/contest/691/problem/D D. Swaps in Permutation You are given a permutation of ...
- Educational Codeforces Round 14 D. Swaps in Permutation 并查集
D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are gi ...
- Swaps in Permutation
Swaps in Permutation You are given a permutation of the numbers 1, 2, ..., n and m pairs of position ...
- codeforces 615 B. Longtail Hedgehog (DFS + 剪枝)
题目链接: codeforces 615 B. Longtail Hedgehog (DFS + 剪枝) 题目描述: 给定n个点m条无向边的图,设一条节点递增的链末尾节点为u,链上点的个数为P,则该链 ...
- codeforces 691D(数据结构)
D. Swaps in Permutation time limit per test 5 seconds memory limit per test 256 megabytes input stan ...
- Educational Codeforces Round 14 D. Swaps in Permutation (并查集orDFS)
题目链接:http://codeforces.com/problemset/problem/691/D 给你n个数,各不相同,范围是1到n.然后是m行数a和b,表示下标为a的数和下标为b的数可以交换无 ...
随机推荐
- Sina App Engine(SAE)入门教程(6)- memcache使用
Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像.视频.文件以及数据库检索的结果等.简单的说就是将数据调用到内 ...
- 网上图书商城项目学习笔记-012BOOK模块查询2
一.分析 > 按图名查询(模糊)(分页)> 按作者查询(分页)> 按出版社查询(分页)> 按id查询> 多条件组合查询(分页) 二.代码 1.view层 (1)gj.js ...
- 解决不安装VC运行库(VC2005,VC2008),程序运行出错的方法
因为VS2005以后程序采用了manifest的生成方式,所以发布的时候要和运行库一起发布.但是我们平时开发和发布的时候如果都要客户安装运行库,那就不太方便了.你可以Microsoft下载:http: ...
- (转)java性能调优
本文转自:http://blog.csdn.net/lilu_leo/article/details/8115612 一.类和对象使用技巧 1.尽量少用new生成新对象 用new创建类的实例时,构造雨 ...
- NSMutableArray 初始化与添加删除程序
Person *person1=[[Person alloc]initWithName:@"Kenshin"]; Person *person2=[[P ...
- 使用Retrofit时出现 java.lang.IllegalArgumentException: URL query string "t={type}&p={page}&size={count}" must not have replace block. For dynamic query parameters use @Query.异常原因
/** * Created by leo on 16/4/30. */ public interface GanchaiService { @GET("digest?t={type}& ...
- shell编程基础(3)条件判断语句
1,带参数的shellscript #this is program build 5.11 to test shell script ############ cxz ####### 5.11 ### ...
- 最受欢迎的ASP.NET的CMS下载
1. Umbraco 项目地址 下载 Umbraco是一个开放源码的CMS内容管理系统,基于asp.net建立,使用mssql进行存储数据.使用Umbraco,设计师能创造出有效的XHTML标记模板和 ...
- Support Library官方教程(3)android studio中导入支援包
Support Library Setup How you setup the Android Support Libraries in your development project depend ...
- 加密解密(9)Diffie-Hellman密钥交换协议
过程如下 : 1,小李把KeyX经过加密变化,生成MsgA传给老王. 2,老王得到MsgA,保存在本地. 3,老王把KeyY经过加密变化,生成MsgB传给小李, 4,小李得到MsgB保存在本地, 5, ...