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

  • 题意:
  • 思路:
  • 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. 【uniapp 开发】智能温控开关 (环状图)

    index.vue <template> <view> <view class="qiun-columns"> <uCharts id=& ...

  2. msmpeng.exe阻止移动硬盘弹出

    MsMpEng.exe 占用 该进程是微软反恶意软件服务的一个可执行文件,用户无法手动停止该进程. 首先运行   eventvwr.msc打开事件查看器,找到警告信息,查看是什么进程在阻止硬盘弹出. ...

  3. java中字符串池,String池,共享池到底是怎么回事?

    栈中有共享池的概念,比 如下面例子中,sz="hello";在栈中创建一个String对象引用变量sz,然后看看栈中有没有"hello",如果没有,则将&quo ...

  4. linux-RHEL7.0 —— 《Linux就该这么学》阅读笔记

    目录 linux-RHEL7.0 安装部署 修改root密码 RPM(红帽软件包管理器) YUM(软件仓库) Systemd初始化进程 总结 linux命令 帮助命令 man 系统工作命令 echo ...

  5. E-R图转换为关系模型

    E-R模型如何转换成关系模型,这里我们分成三种情况进行讲解,分别是一对一,一对多和多对多. 1.一对一的情况: 有两种方法解决这个问题.第一个方法:可以单独对应一个关系模式,由各实体的主码构成关系模式 ...

  6. 小程序滚动事件之头部渐隐渐现demo

    效果图: ==>  代码: //test1.wxml <view class='header' style="opacity:{{opacityStyle}}" hid ...

  7. Mybatis实现多级菜单查询

    写在前面 最近实现一个小需求,前端需要菜单的信息,需要向后端发起获取菜单的请求,菜单又是一个多级菜单,后端我用的mybatis进行数据库查询,实现的方法我这里想到有两种,欢迎大家补充. 1. 在Men ...

  8. Mycat管理

    Mycat 提供了类似数据库的管理监控方式,可以通过 mycat 端口 9066 执行相应的 SQL 语句进行管理,可以可以通过 navicat 软件进行远程连接管理,再使用 MySQL 命令行界面登 ...

  9. git的.gitignore文件内容

    **/pom.xml.versionsBackup **/target/ **/out/ *.class # Mobile Tools for Java (J2ME) .mtj.tmp/ .idea/ ...

  10. 听说Integer有bug?1000不等于1000?

    bug? 前几天有位朋友找我,说:"老哥,老哥,我好像发现了Integer一个bug,你帮我看看什么情况?",说完给了我两个很简单的demo,上代码. 100 == 100 100 ...