Educational Codeforces Round 100 (Rated for Div. 2) E. Plan of Lectures 缩点+拓扑排序

E. Plan of Lectures

Ivan is a programming teacher. During the academic year, he plans to give 𝑛 lectures on 𝑛 different topics. Each topic should be used in exactly one lecture. Ivan wants to choose which topic will he explain during the 1-st, 2-nd, …, 𝑛-th lecture — formally, he wants to choose some permutation of integers from 1 to 𝑛 (let’s call this permutation 𝑞). 𝑞𝑖 is the index of the topic Ivan will explain during the 𝑖-th lecture.

For each topic (except exactly one), there exists a prerequisite topic (for the topic 𝑖, the prerequisite topic is 𝑝𝑖). Ivan cannot give a lecture on a topic before giving a lecture on its prerequisite topic. There exists at least one valid ordering of topics according to these prerequisite constraints.

Ordering the topics correctly can help students understand the lectures better. Ivan has 𝑘 special pairs of topics (𝑥𝑖,𝑦𝑖) such that he knows that the students will understand the 𝑦𝑖-th topic better if the lecture on it is conducted right after the lecture on the 𝑥𝑖-th topic. Ivan wants to satisfy the constraints on every such pair, that is, for every 𝑖∈[1,𝑘], there should exist some 𝑗∈[1,𝑛−1] such that 𝑞𝑗=𝑥𝑖 and 𝑞𝑗+1=𝑦𝑖.

Now Ivan wants to know if there exists an ordering of topics that satisfies all these constraints, and if at least one exists, find any of them.

Input

The first line contains two integers 𝑛 and 𝑘 (2≤𝑛≤3⋅105, 1≤𝑘≤𝑛−1) — the number of topics and the number of special pairs of topics, respectively.

The second line contains 𝑛 integers 𝑝1, 𝑝2, …, 𝑝𝑛 (0≤𝑝𝑖≤𝑛), where 𝑝𝑖 is the prerequisite topic for the topic 𝑖 (or 𝑝𝑖=0 if the 𝑖-th topic has no prerequisite topics). Exactly one of these integers is 0. At least one ordering of topics such that for every 𝑖 the 𝑝𝑖-th topic is placed before the 𝑖-th topic exists.

Then 𝑘 lines follow, the 𝑖-th line contains two integers 𝑥𝑖 and 𝑦𝑖 (1≤𝑥𝑖,𝑦𝑖≤𝑛; 𝑥𝑖≠𝑦𝑖) — the topics from the 𝑖-th special pair. All values of 𝑥𝑖 are pairwise distinct; similarly, all valus of 𝑦𝑖 are pairwise distinct.

Output

If there is no ordering of topics meeting all the constraints, print 0.

Otherwise, print 𝑛 pairwise distinct integers 𝑞1, 𝑞2, …, 𝑞𝑛 (1≤𝑞𝑖≤𝑛) — the ordering of topics meeting all of the constraints. If there are multiple answers, print any of them.

Examples

5 2

2 3 0 5 3

1 5

5 4

3 2 1 5 4

有一个人在讲课,会讲n门课。

对于每门课i,他必须在第pi门课讲完后才能讲。其中仅有一门课的pi为0,表示没有需要提前讲的。保证不成环。

现在还有k个强条件,每个条件会给xi,yi。表示讲第xi门课后的下一节课必须讲第yi门课;保证xi!=xj,yi!=yj。

问你是否有合法解,有的话就任意输出一个即可。

首先这道题的题意需要理解一下:

第一个条件相当于给了你一棵树。

第二个条件给了你若干条链或者环,如果存在环直接输出非法即可。(一定是链或者环,因为xi!=xj,yi!=yj,不会有多个点指向同一个点)。

然后这道题因为链里面的点必须强制按照顺序输出,那么我们直接将链进行缩点,然后将树根据缩点后的结果变成一个DAG,然后跑一个拓扑排序直接输出即可。

非法的情况是:强条件中存在环;强条件中顺序和树上顺序冲突;形成的DAG里面有环。

#include<bits stdc++.h>
using namespace std;
const int maxn = 3e5+7;
vector<int>G[maxn];
int n,k,fa[maxn],nxt[maxn];
int L[maxn],id,rnk[maxn],vis[maxn],rt[maxn],R,ind[maxn];
int main() {
    cin>>n>>k;
    for (int i=1;i<=n;i++) { int x;cin>>x;
        fa[i]=x;
        if (x==0) R=i;
    }
    for (int i=1;i<=k;i++) { int x,y; cin>>x>>y;
        nxt[x]=y;
        vis[y]=1;
    }
    for (int i=1;i<=n;i++) { if (vis[i]) continue; l[i]="++id;" int now="0,x=nxt[i];" rnk[i]="now;" rt[id]="i;" while(x!="0&&L[x]==0)" l[x]="id;" rnk[x]="++now;" x="nxt[x];" } check circle pass for (int i="1;i<=n;i++)" (!l[i]) cout<<"0"<<endl; return 0; (l[i]="=L[fa[i]]&&rnk[i]<rnk[fa[i]])" (fa[i]="=0)continue;" (l[fa[i]] !="L[i])" g[l[fa[i]]].push_back(l[i]); ind[l[i]]++; vector<int> ans;
    queue<int> Q;
    for (int i=1;i<=id;i++) { if (ind[i]="=0)" q.push(i); } while(!q.empty()) int now="Q.front();" q.pop(); ans.push_back(now); for (int i="0;i<G[now].size();i++)" v="G[now][i];" ind[v]--; (ind[v]="=0)" q.push(v); (ans.size()="=id)" cout<<"---- " << ans[i]<< rt[ans[i]] endl; id="L[rt[ans[i]]];" while(now&&id="=L[now])" cout<<now<<" "; cout<<endl; return 0; cout<<"0"<<endl; < code></=id;i++)></int></=n;i++)></=k;i++)></=n;i++)></int></bits>

Original: https://www.cnblogs.com/qscqesze/p/14157863.html
Author: qscqesze
Title: Educational Codeforces Round 100 (Rated for Div. 2) E. Plan of Lectures 缩点+拓扑排序

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/553172/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 设计模式概述(一)

    设计模式(Design Pattern)是前辈们对代码开发经验的总结,是解决特定问题的一系列套路。它不是语法规定,而是一套用来提高代码可复用性、可维护性、可读性、稳健性以及安全性的…

    技术杂谈 2023年7月11日
    066
  • 微服务组件–限流框架SpringCloudHystrix详解

    Hystrix的介绍 【1】Hystrix是springCloud的组件之一,Hystrix 可以让我们在分布式系统中对服务间的调用进行控制加入一些调用延迟或者依赖故障的容错机制。…

    技术杂谈 2023年7月23日
    089
  • gerrit系统如何配置访问控制

    .版本:v0.3作者:河东西望日期:2022-7-13. gerrit系统的上手使用有两个难点: 想要上手使用gerrit的同仁们,搭建部署好gerrit系统之后,会发现gerri…

    技术杂谈 2023年6月21日
    085
  • Linux的NIS配置

    快速命令 Server和Client设置NIS域名 nisdomainname nis_server echo ‘NISDOMAIN=nis_server’ >> /e…

    技术杂谈 2023年6月21日
    077
  • Redis基础

    Redis Redis介绍和安装 redis 是一个非关系型数据库(区别于mysql关系型数据库,关联关系,外键,表),nosql数据库(not only sql:不仅仅是SQL)…

    技术杂谈 2023年6月21日
    0109
  • Kubenetes 拓扑图

    Kubenetes 拓扑图 posted @2021-03-07 14:27 kevin.Xiang 阅读(272 ) 评论() 编辑 Original: https://www….

    技术杂谈 2023年6月1日
    098
  • 搭建Redis一主两从三哨兵

    实践 – 搭建Redis一主两从三哨兵 原因: 最近在复习Redis的时候,学习到了为了提高Redis集群的 高可用性,有一个模式为 哨兵模式。 哨兵模式的作用是为了在…

    技术杂谈 2023年6月21日
    0102
  • 是时候使用 YAML 来做配置或数据文件了

    概述 我们做程序,经常需要用到配置信息,回顾一下这么多年的搬砖生涯,我记得用过多种格式的文件来定义配置信息,例如 ini&#x6587;&#x4EF6;, xml&…

    技术杂谈 2023年5月31日
    089
  • localstorage 过期时间

    很遗憾,localstorage原生是不支持设置过期时间的,想要设置的话,就只能自己来封装一层逻辑来实现: function set(key,value){ var curtime…

    技术杂谈 2023年5月31日
    087
  • MySQL InnoDB索引原理

    数据库与I/O原理 数据会持久化到磁盘,查询数据是就会有I/O操作,相对于缓存操作,I/O操作的时间成本相当高昂。 I/O操作的基本单位是一个磁盘页面,比如16KB的页面大小。当数…

    技术杂谈 2023年6月21日
    099
  • SSH免密登录

    SSH免密登录实现三步: 客户端生成公钥和私钥 上传公钥到服务端 SSH免密登录 (1) 客户端生成和公钥和私钥 ssh-keygen 一路回车即可,默认会在~/.ssh/目录下创…

    技术杂谈 2023年7月11日
    068
  • go-select 机制

    select 的用法与 switch 语言非常类似,由 select 开始一个新的选择块,每个选择条件由 case 语句来描述。 与 switch 语句相比,select 有比较多…

    技术杂谈 2023年7月11日
    065
  • 浅谈kali : arpspoof工具原理

    介绍 arpspoof是一个通过ARP协议伪造数据包实现中间人攻击的kali工具。 中间人攻击虽然古老,但仍处于受到黑客攻击的危险中,可能会严重导致危害服务器和用户。仍然有很多变种…

    技术杂谈 2023年7月25日
    082
  • 一款非常棒的十六进制编辑器 —— 010 Editor

    参考 https://zhuanlan.zhihu.com/p/96001673 插件 ELF.bt 用来分析ELF文件,用起来感觉像wireshark,可以高亮源文件中正常查看的…

    技术杂谈 2023年5月31日
    0112
  • Linux 搭建Apollo

    简介 Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用…

    技术杂谈 2023年7月11日
    084
  • Linux命令拾遗-使用blktrace分析io情况

    原创:打码日记(微信公众号ID:codelogs),欢迎分享,转载请保留出处。 简介 一般来说,想检查磁盘I/O情况,可以使用iostat、iotop、sar等,但这些命令只能做一…

    技术杂谈 2023年7月24日
    083
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球