当前位置:网站首页>Examples of topological sequences
Examples of topological sequences
2022-07-01 00:06:00 【Zhuo Mu is not a woodpecker】
subject :
Given a n A little bit m Directed graph of strip edge , 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, Indication point x Sum point y There is a directed edge between (x, y).
Output format
All in one line , If there is a topological sequence , Then the topology sequence is output .
Otherwise output -1.
Data range
1≤n,m≤10^5
sample input :
3 3
1 2
2 3
1 3sample output :
1 2 3Code :
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=1e5+10;
int h[N],e[N],ne[N],idx,q[N],d[N],n,m,b[N];
void add(int a,int b){
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
bool topsort(){
int hh=0,tt=-1;// Define the head and tail of the team
for(int i=1;i<=n;i++){
if(!d[i]) q[++tt]=i;// Take out all the data with zero queue and save it in q
}
while(hh<=tt){
int v=q[hh++];
for(int i=h[v];i!=-1;i=ne[i]){
int w=e[i];
d[w]--;// Take out a number , Join the team minus one
if(!d[w]) q[++tt]=w;// If the enrollment is zero, deposit q
}
}
if(tt==n-1){
for(int i=0;i<n;i++) cout<<q[i]<<" ";
cout<<endl;
}
}
int main(){
cin>>n>>m;
memset(h,-1,sizeof h);
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;
add(a,b);
d[b]++;
}
topsort();
return 0;
}边栏推荐
- VR panorama adds contrast function to make the display of differentiation effect more intuitive!
- Fh6908a negative pole turn off synchronous rectification analog low voltage drop diode control IC chip tsot23-6 ultra low power rectifier 1W power consumption < 100ua static replacement mp6908
- Rust controls Dajiang programmable UAV Tello
- Summer Challenge [FFH] harmonyos mobile phone remote control Dayu development board camera
- When is it appropriate to replace a virtual machine with a virtual machine?
- Understand target detection in one article: r-cnn, fast r-cnn, fast r-cnn, Yolo, SSD "suggestions collection"
- How to edit special effects in VR panorama? How to display detailed functions?
- Ditto set global paste only text shortcuts
- 异步過渡方案—Generator
- Maxpool2d explanation -- Application in arrays and images
猜你喜欢

Combining online and offline, VR panorama is a good way to transform furniture online!

Development of wireless U-shaped ultrasonic electric toothbrush

Shell multitasking to download video at the same time

206页上海BIM技术应用与发展报告2021

Vmware16 installing win11 virtual machine (the most complete step + stepping on the pit)
![[UML] UML class diagram](/img/6f/30bd15967103969e600d69e618d8bf.png)
[UML] UML class diagram

What is SRM system and how to standardize the internal procurement process of the company

How to edit special effects in VR panorama? How to display detailed functions?

5G智慧建筑解决方案2021

让企业数字化砸锅和IT主管背锅的软件供应链安全风险指北
随机推荐
Sm2246en+ SanDisk 15131
Rust book materials - yazhijia Library
Ctfshow permission maintenance
基金客户服务
Repetition is the mother of skill
6-1 exploit -ftp exploit
Manage edge browser settings (ie mode, homepage binding, etc.) through group policy in the enterprise
Is it safe to choose mobile phone for stock trading account opening in Guangzhou?
Fund sales code of conduct and information management
Combining online and offline, VR panorama is a good way to transform furniture online!
C /platform:anycpu32bitpererrored can only be used with /t:exe, /t:winexe and /t:appcontainerexe
The full technology stack, full scene and full role cloud native series training was launched to help enterprises build a hard core cloud native technology team
The difference between union and union all in MySQL
8253A寄存器浅析
Tide - rust web framework based on async STD
New trends of China's national tide development in 2022
Gateway service gateway
VR panorama adds contrast function to make the display of differentiation effect more intuitive!
File reading and writing for rust file system processing - rust Practice Guide
Advanced mathematical modeling