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 ...
随机推荐
- React 可视化开发工具 Shadow Widget 非正经入门(之五:指令式界面设计)
本系列博文从 Shadow Widget 作者的视角,解释该框架的设计要点.本篇解释 Shadow Widget 中类 Vue 的控制指令,与指令式界面设计相关. 1. 指令式界面设计 Vue 与 A ...
- 【uniapp 开发】工具类 -- MathUtil
获取随机数 /* ** randomWord 产生任意长度随机字母数字组合 ** randomFlag 是否任意长度 min 任意长度最小位[固定位数] max 任意长度最大位 ** ** 生成6-1 ...
- PAT B1042 字符统计
请编写程序,找出一段给定文字中出现最频繁的那个英文字母. 输入格式: 输入在一行中给出一个长度不超过 1000 的字符串.字符串由 ASCII 码表中任意可见字符及空格组成,至少包含 1 个英文字母, ...
- flex布局控制最后一个元素右浮动
可以在最后一个元素添加css属性 margin-left: auto; 例如我一排排列的元素 ,子元素并没有完全排列撑开父元素的宽度,这时候要使最后一个元素想最右 可以让最后一个元素的 margin- ...
- 攻防世界——stegano
分析 1. 一个pdf,里边都是英文. 打开pdf "ctrl + F",检查flag 然活这里边直接告诉你,flag不在这里,一般都这么说了那就是真的不在了. 2. txt打开, ...
- 控制反转 IOC 理论推导
控制反转 IOC 理论推导 按照我们传统的开发,我们会先去 dao 层创建一个接口,在接口中定义方法. public interface UserDao { void getUser(); } 然后再 ...
- Java学习——数组的基础知识
数组的特点.分类:一维.二维数组的使用:数组的声明和初始化.调用数组的指定位置的元素.获取数组的长度.遍历数组.数组元素的默认初始化值
- Unity通过脚本创建Mesh(网格)
##1.创建一个带Mesh的物体 Unity中的网格作为组件不能脱离物体单独存在 新建脚本CreateMesh public class CreateMesh: MonoBehaviour { voi ...
- C/C++在Win32控制台播放Bad Apple
##前言 这里首先你需要准备一些文件,将一个Bad Apple的视频分别转换成txt和mp3格式(mp3用来作为背景音乐) 我将txt文件放到exe文件目录下的子目录files里了 转换方法可以用Ad ...
- vue 滚动条样式设置
App.vue 文件下加入下面css // 滚动条宽度 ::-webkit-scrollbar{ width: 6px; } /* 定义滚动条轨道 */ ::-webkit-scrollb ...