CF1682E Unordered Swaps
鸽着,我不知道为什么对?
- 题意:
- 思路:
- code:
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
int p[N],nxt[N],to[N],head[N],ecnt,d[N];
void add_edge(int u,int v,int i) {nxt[++ecnt]=head[u];d[ecnt]=i;to[ecnt]=v;head[u]=ecnt;}
int fa[N],dep[N],In[N],id[N];
void init(int u) {
for(int i=head[u];i;i=nxt[i]) {
int v=to[i];if(v==fa[u])continue;
dep[v]=dep[u]+1;fa[v]=u;id[v]=d[i];
init(v);
}
}
vector<int> R[N];
void Go_up(int x,int y) {
int u=x,v=y,lx=0,ly=0;
while(u!=v) {
if(dep[u]>dep[v]) {
if(lx){R[lx].push_back(id[u]);In[id[u]]++;}
lx=id[u];u=fa[u];
}
else {
if(ly) {R[id[v]].push_back(ly);In[ly]++;}
ly=id[v];v=fa[v];
}
}
if(lx&&ly) {R[lx].push_back(ly);In[ly]++;}
}
int hd=1,tl,Q[N];
int main() {
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) {scanf("%d",&p[i]);}
for(int i=1;i<=m;i++) {int x,y;scanf("%d%d",&x,&y);add_edge(x,y,i),add_edge(y,x,i);}
for(int i=1;i<=n;i++) if(!dep[i])init(i);
// printf("!");
for(int i=1;i<=n;i++) {if(i!=p[i])Go_up(i,p[i]);}
for(int i=1;i<=m;i++) if(!In[i])Q[++tl]=i;
while(hd<=tl) {
int u=Q[hd++];
printf("%d ",u);
for(int i=0;i<R[u].size();i++) {
int v=R[u][i];
if(!--In[v]) {Q[++tl]=v;}
}
}
return 0;
}
CF1682E Unordered Swaps的更多相关文章
- Codeforces Round #793 (Div. 2)
C. LIS or Reverse LIS? D. Circular Spanning Tree E. Unordered Swaps F MCMF?
- Minimum number of swaps required to sort an array
https://www.hackerrank.com/challenges/minimum-swaps-2/problem Minimum Swaps II You are given an unor ...
- Bitset<>用于unordered container时的默认hash函数
自从c++11起,bitset用于unordered container,将会提供默认的hash函数. 在gcc中,相关代码如下: // DR 1182. /// std::hash speciali ...
- Serializable unordered set
Serializable unordered set 可序列化哈希set #include <boost/algorithm/string/predicate.hpp> #include ...
- unordered容器
1.散列容器(hash container) 散列容器通常比二叉树的存储方式可以提供更高的访问效率. #include <boost/unordered_set.hpp> #includ ...
- [codeforces 339]E. Three Swaps
[codeforces 339]E. Three Swaps 试题描述 Xenia the horse breeder has n (n > 1) horses that stand in a ...
- uva331 - Mapping the Swaps
Mapping the Swaps Sorting an array can be done by swapping certain pairs of adjacent entries in the ...
- UVA Mapping the Swaps
题目例如以下: Mapping the Swaps Sorting an array can be done by swapping certain pairs of adjacent entrie ...
- Three Swaps DFS
E. Three Swaps time limit per test 1 second memory limit per test 256 megabytes input standard input ...
随机推荐
- es6零碎记忆
1:substring 和 substr var str = '0123456789' console.log(str.substring(1)); //123456789 console.log(s ...
- ES6-11学习笔记--对象的扩展
属性简洁表示法 属性名表达式 Objec.is() 扩展运算符 与 Object.assign() in 对象的遍历方式 属性简洁表示法: 如果属性key跟变量名一样,可简写 let name = ...
- wx:key报错does not look like a valid key name
把花括号去掉就行了, 现在改版了, 要注意了 wx:key="index"
- 微信分享报错config:invalid url domain
错误类型:invalid url domain 调试返回参数: { "errMsg": "config:invalid url domain" } 截图: 环境 ...
- 新版vue作用域插槽的使用
2.6开始,作用域插槽的使用有了不同的地方: 作用域插槽的个人理解就是让子组件的数据可以在父组件中使用: 也是一个数据传递的方式了: 不多说,上代码 子组件定义一个插槽,并且定义一个需要传递到父组件 ...
- Mysql各版本号的含义
1)MySQL Community Server 社区版本,开源免费,但不提供官方技术支持2)MySQL Enterprise Edition企业版本,需付费,可以试用30天3)MySQL Clust ...
- springboot读取配置文件赋值给静态变量
1.实现InitializingBean接口,重写afterPropertiesSet方法,将@Value赋值给成员变量的属性赋值给静态变量,示例如下: /** * @Classname FileUt ...
- echarts饼图去除圈外指向横线
series: [ { name: '实时人员信息', type: 'pie', radius: ['86%', '80%'], avoidLabelOverlap: false, label: { ...
- 不仅仅是一把瑞士军刀 —— Apifox的野望和不足
声明:本文内容不涉及任何 Apifox 的功能介绍,一来网上这方面的文章已经汗牛充栋,二来 Apifox 本身的用户体验做的非常好,对于开发者而言学习成本基本为零. 阮一峰:不管你是前端开发还是后端开 ...
- 关于 display: inline-block; 中间有间隙的问题
当我们给一个元素的一系列子元素设置display: inline-block; 时,会发现子元素之间存在间隙,如 <style> div { display: inline-block; ...