当前位置:网站首页>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
- [wp]bmzclub几道题的writeup
- 汇编-入门
- Special Edition: spreadjs v15.1 vs spreadjs v15.0
- 【软件逆向-分析工具】反汇编和反编译工具
- UI自動化測試從此告別手動下載瀏覽器驅動
- Solve the problem that sqlyog does not have a schema Designer
- MindFusion. Virtual Keyboard for WPF
- glibc strlen 实现方式分析
- Google Chrome CSS will not update unless the cache is cleared - Google Chrome CSS doesn't update unless clear cache
猜你喜欢
Easy processing of ten-year futures and stock market data -- Application of tdengine in Tongxinyuan fund
Containerization Foundation
【刷题】BFS题目精选
[an Xun cup 2019] not file upload
The new project Galaxy token just announced by coinlist is gal
Plasticscm enterprise crack
CTF stegano practice stegano 9
error Couldn‘t find a package. JSON file in "your path“
[web Audit - source code disclosure] obtain source code methods and use tools
Soul 3: what is interface testing, how to play interface testing, and how to play interface automation testing?
随机推荐
Monitoring web performance with performance
How about programmers' eyesight| Daily anecdotes
UI自動化測試從此告別手動下載瀏覽器驅動
DECLARE_ WAIT_ QUEUE_ HEAD、wake_ up_ Interruptible macro analysis
Analysis of dagger2 principle
NEW:Devart dotConnect ADO.NET
【软件逆向-分析工具】反汇编和反编译工具
[groovy] string (string injection function | asBoolean | execute | minus)
反絮凝剂-氨碘肽滴眼液
Redis之Jedis如何使用
IronXL for . NET 2022.6
Test d'automatisation de l'interface utilisateur télécharger manuellement le pilote du navigateur à partir de maintenant
How is the entered query SQL statement executed?
灵魂三问:什么是接口测试,接口测试怎么玩,接口自动化测试怎么玩?
ABP vNext microservice architecture detailed tutorial - distributed permission framework (Part 1)
English essential vocabulary 3400
【web源码-代码审计方法】审计技巧及审计工具
[web source code code code audit method] audit skills and tools
[charging station]_ Secular wisdom_ Philosophical wisdom _
Redis6-01nosql database