鸽着,我不知道为什么对?

  • 题意:
  • 思路:
  • 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的更多相关文章

  1. Codeforces Round #793 (Div. 2)

    C. LIS or Reverse LIS? D. Circular Spanning Tree E. Unordered Swaps F MCMF?

  2. 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 ...

  3. Bitset<>用于unordered container时的默认hash函数

    自从c++11起,bitset用于unordered container,将会提供默认的hash函数. 在gcc中,相关代码如下: // DR 1182. /// std::hash speciali ...

  4. Serializable unordered set

    Serializable unordered set 可序列化哈希set #include <boost/algorithm/string/predicate.hpp> #include ...

  5. unordered容器

    1.散列容器(hash container)  散列容器通常比二叉树的存储方式可以提供更高的访问效率. #include <boost/unordered_set.hpp> #includ ...

  6. [codeforces 339]E. Three Swaps

    [codeforces 339]E. Three Swaps 试题描述 Xenia the horse breeder has n (n > 1) horses that stand in a ...

  7. uva331 - Mapping the Swaps

    Mapping the Swaps Sorting an array can be done by swapping certain pairs of adjacent entries in the ...

  8. UVA Mapping the Swaps

    题目例如以下: Mapping the Swaps  Sorting an array can be done by swapping certain pairs of adjacent entrie ...

  9. Three Swaps DFS

    E. Three Swaps time limit per test 1 second memory limit per test 256 megabytes input standard input ...

随机推荐

  1. Web缓存总结

    web缓存作用 减少网络带宽消耗降低服务器压力减少网络延迟,加快页面打开速度 Web缓存的类型 数据库数据缓存:为了提供查询的性能,会将查询后的数据放到内存中进行缓存,下次查询时,直接从内存缓存直接返 ...

  2. 大数据学习之路之ambari的安装

    之前按照正常方式安装的hbase不能插入数据 所以今天来尝试下ambari能不能行 已经打了快照 如果不能还能恢复之前的样子

  3. java语言和jdk、jre基础

    Java语言平台 * J2SE(Java 2 Platform Standard Edition)标准版  * 是为开发普通桌面和商务应用程序提供的解决方案,该技术体系是其他两者的基础,可以完成一些桌 ...

  4. vue-cropper裁剪上传

    效果图: 全部代码: npm install vue-cropper //首先 安装vue-cropper main.js全局引用: import VueCropper from 'vue-cropp ...

  5. 微信小程序获取当前时间戳、获取当前时间、时间戳加减

    //获取当前时间戳 var timestamp = Date.parse(new Date()); timestamp = timestamp / 1000; console.log("当前 ...

  6. Java类型转换详解

    Java类型转换详解 最近有同学问:自动类型转换老是记不住,到底是大转小,还是小转大 其实这个不用死记硬背,很好理解,我们拿 int 和 short 来举例: int 是 4 字节,也就是 32 bi ...

  7. 微信小程序--设置和获取剪切板内容

    设置 wx.setClipboardData  // 复制功能 获取 wx.getClipboardData // 粘贴功能     let _this = this     wx.setClipbo ...

  8. QQ浏览器X5内核问题汇总 转

    常常被人问及微信中使用的X5内核的问题,其实我也不是很清楚,只知道它是基于android 4.2的webkit,版本号是webkit 534.今天正好从X5团队拿到了一份问题汇总,梳理下发出来,给各位 ...

  9. Service vs Factory vs provider的迷惑

    刚开始我很迷惑的,但是经过一段时间的项目,还有看大漠老师的东西,似乎明白了,他们的区别也就是  一个人喜欢吃面还是吃饭或者肯德基区别.目的就是填饱肚子! 以下是它们在AngularJS源代码中的定义: ...

  10. JavaScript学习④

    * BOM 1. 事件 ## DOM简单学习:为了满足案例要求 * 功能:控制html文档的内容 * 获取页面标签(元素)对象:Element * document.getElementById(&q ...