当前位置:网站首页>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;
}
边栏推荐
- Analysis of glibc strlen implementation mode
- Monitoring web performance with performance
- [summary of two registration methods]
- 【刷题】BFS题目精选
- Containerization Foundation
- Zero foundation uses paddlepaddle to build lenet-5 network
- Deep learning - LSTM Foundation
- Smart pointer shared_ PTR and weak_ Difference of PTR
- Interview byte, pass the exam and directly work on three sides. As a result, I found an architect to hang me?
- [system security] ten thousand words summary system virtualization container bottom layer principle experiment
猜你喜欢
![[C language] address book - dynamic and static implementation](/img/eb/07e7a32a172e5ae41457cf8a49c130.jpg)
[C language] address book - dynamic and static implementation

线上故障突突突?如何紧急诊断、排查与恢复

NEW:Devart dotConnect ADO.NET
![[wp][入门]刷弱类型题目](/img/d0/9eb3ade701057837d98e4a20082a10.png)
[wp][入门]刷弱类型题目
![[an Xun cup 2019] not file upload](/img/f1/736eb5fe51c299e3152ca87895ee99.png)
[an Xun cup 2019] not file upload

UI自动化测试从此告别手动下载浏览器驱动
![[positioning in JS]](/img/f1/02ce74fadc1f7524c7abca9db66c71.jpg)
[positioning in JS]

error Couldn‘t find a package. JSON file in "your path“

程序员的视力怎么样? | 每日趣闻
![[web Audit - source code disclosure] obtain source code methods and use tools](/img/ea/84e67a1fca0e12cc4452c744c242b4.png)
[web Audit - source code disclosure] obtain source code methods and use tools
随机推荐
Enterprise level: spire Office for . NET:Platinum|7.7. x
UI自动化测试从此告别手动下载浏览器驱动
Subversive cognition: what does SRE do?
JWT漏洞复现
Delphi free memory
[positioning in JS]
speed or tempo in classical music
[wp][introduction] brush weak type questions
Blue Bridge Cup single chip microcomputer -- PWM pulse width modulation
[charging station]_ Secular wisdom_ Philosophical wisdom _
Test d'automatisation de l'interface utilisateur télécharger manuellement le pilote du navigateur à partir de maintenant
[understand series after reading] 6000 words teach you to realize interface automation from 0 to 1
MindFusion.Virtual Keyboard for WPF
优先使用对象组合,而不是类继承
一文带你了解BI的前世今身与企业数字化转型的关系
【软件逆向-分析工具】反汇编和反编译工具
Deflocculant aminoiodotide eye drops
Kubernetes - Multi cluster management
IronXL for .NET 2022.6
MySQL winter vacation self-study 2022 11 (9)