当前位置:网站首页>BDF application - topology sequence
BDF application - topology sequence
2022-07-05 03:56:00 【jigsaw_ zyx】
Problem description
Given a n A little bit m Directed graph of strip edge , The number of points is 1 To n, There may be double edges and self rings in the graph .
Please output any topological sequence of the directed graph , If the topological sequence does not exist , The output -1.
If a sequence consisting of all the points in the graph A Satisfy : For each edge in the graph (x, y),x stay A All appear in y Before , said A Is a topological sequence of the graph .
Input format
The first line contains two integers n and m
Next m That's ok , Each line contains two integers x and y, Indicates that there is a from point x point-to-point y The directed side of (x, y).
Output format
All in one line , If there is a topological sequence , Then output any legal topological sequence .
Otherwise output -1.
Data range
1≤n,m≤105
sample input :
3 3
1 2
2 3
1 3
sample output :
1 2 3
Code
#include<bits/stdc++.h>
using namespace std;
const int N=100100;
int n,m;
int h[N],e[N],ne[N],idx=0;// Linked list record chart
int q[N],d[N];// Simulation queue 、 Record the degree of penetration
void add(int a,int b){
// Save map
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
bool topsort(){
// A topological sort
int hh=0,tt=-1;
for(int i=1;i<=n;i++)
if(d[i]==0)
q[++tt]=i;
while(hh<=tt){
int k=q[hh++];
for(int i=h[k];i!=-1;i=ne[i]){
int j=e[i];
d[j]--;
if(d[j]==0) q[++tt]=j;
}
}
return tt==n-1;
}
int main(){
cin>>n>>m;
memset(h,-1,sizeof h);
while(m--){
int x,y;
cin>>x>>y;
add(x,y);
d[y]++;
}
if(topsort()){
for(int i=0;i<n;i++) cout<<q[i]<<" ";
puts("");
}else cout<<-1;
return 0;
}
边栏推荐
- 一文带你了解BI的前世今身与企业数字化转型的关系
- v-if VS v-show 2.0
- EasyCVR平台出现WebRTC协议视频播放不了是什么原因?
- [groovy] string (string injection function | asBoolean | execute | minus)
- 优先使用对象组合,而不是类继承
- Soul 3: what is interface testing, how to play interface testing, and how to play interface automation testing?
- 面试字节,过关斩将直接干到 3 面,结果找了个架构师来吊打我?
- [vérification sur le Web - divulgation du code source] obtenir la méthode du code source et utiliser des outils
- 企业级:Spire.Office for .NET:Platinum|7.7.x
- 请问一下我的请求是条件更新,但在buffer中就被拦截了,这种情况我只能每次去flush缓存么?
猜你喜欢

DMX parameter exploration of grandma2 onpc 3.1.2.5

An elegant program for Euclid‘s algorithm

Interview summary: This is a comprehensive & detailed Android interview guide

Installation of postman and postman interceptor
![[luat-air105] 4.1 file system FS](/img/5e/7fdeedaef420736d761f4a681cd2d8.jpg)
[luat-air105] 4.1 file system FS

Containerization Foundation

Resolved (sqlalchemy+pandas.read_sql) attributeerror: 'engine' object has no attribute 'execution_ options‘

IronXL for . NET 2022.6
![[vérification sur le Web - divulgation du code source] obtenir la méthode du code source et utiliser des outils](/img/ea/84e67a1fca0e12cc4452c744c242b4.png)
[vérification sur le Web - divulgation du code source] obtenir la méthode du code source et utiliser des outils

C语言课设:影院售票管理系统
随机推荐
C # use awaiter
面试汇总:这是一份全面&详细的Android面试指南
Containerization Foundation
Basic knowledge of tuples
Zero foundation uses paddlepaddle to build lenet-5 network
天干地支纪年法中为什么是60年一个轮回,而不是120年
How to make the listbox scroll automatically when adding a new item- How can I have a ListBox auto-scroll when a new item is added?
【web源码-代码审计方法】审计技巧及审计工具
PlasticSCM 企业版Crack
[数组]566. 重塑矩阵-简单
laravel8 导出Excle文件
Subversive cognition: what does SRE do?
一文带你了解BI的前世今身与企业数字化转型的关系
Multimedia query
v-if VS v-show 2.0
An elegant program for Euclid‘s algorithm
Use of vscode software
DECLARE_ WAIT_ QUEUE_ HEAD、wake_ up_ Interruptible macro analysis
25K 入职腾讯的那天,我特么哭了
Nmap使用手册学习记录