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 ...
随机推荐
- java中的排序除了冒泡以来, 再给出一种方法, 举例说明
9.5 排序: 有一种排序的方法,非常好理解,详见本题的步骤,先找出最大值和最小值,把最小值打印出来后,把它存在另一个数组b当中,再删除此最小值,之后再来一次找出最小值,打印出最小值以后,再把它存 ...
- 预排序遍历算法(MPTT)
预排序遍历算法(MPTT) 算法详细: 对于所有的树的节点,都会有一个左值和一个右值,用于确定该节点的边界. 父节点的左值都会比子节点左值的小,右值都会比子节点的右值大. 没有父节点新增:即没有父节点 ...
- ABP源码分析 - 服务配置(1)
比较随意,记录下过程,以便忘了以后重拾. 三个关注点 Program.cs internal static IHostBuilder CreateHostBuilder(string[] args) ...
- DOCTYPE(⽂档类型) 的作⽤
DOCTYPE是HTML5中一种标准通用标记语言的文档类型声明,它的目的是告诉浏览器(解析器)应该以什么样(html或xhtml)的文档类型定义来解析文档,不同的渲染模式会影响浏览器对 CSS 代码甚 ...
- Struct2中三种获取表单数据的方式
1.使用ActionContext类 //1获取ActionContext对象 ActionContext context = ActionContext.getContext(); //2.调用方法 ...
- 使用 LOAD DATA LOCAL INFILE,sysbench 导数速度提升30%
1. LOAD DATA INFILE 为什么比 INSERT 快? 2. sysbench 压测 MySQL 的四个标准步骤. 3. 怎么让 sysbench 支持 LOAD DATA LOCAL ...
- QT-notepad++仿写
最近小忙,准备学习下FFMPEG 涉及:工具栏使用,QAction,文件基本读写操作 Github地址:https://github.com/wsdassssss/notepad-
- 安卓记账本开发学习day8之导入外部依赖
以要使用的柱状图分析显示为例,项目文件夹最外层的build.gradle,加入下列语句 allprojects { repositories { google() jcenter() maven { ...
- jsp第七周作业
1.p78-p79的例4-9 <%@ page language="java" import="java.util.*" pageEncoding=&qu ...
- contact 和 footer 的页面制作
1. html 结构 <section id="contact"> <div class="contact-form bg-primary p-2&qu ...