当前位置:网站首页>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;
}
边栏推荐
- [wp][introduction] brush weak type questions
- 为什么百度、阿里这些大厂宁愿花25K招聘应届生,也不愿涨薪5K留住老员工?
- 线程基础知识
- New interesting test applet source code_ Test available
- PlasticSCM 企业版Crack
- 阿里云ECS使用cloudfs4oss挂载OSS
- De debugging (set the main thread as hidden debugging to destroy the debugging Channel & debugger detection)
- Technology sharing swift defense programming
- MindFusion.Virtual Keyboard for WPF
- How to use jedis of redis
猜你喜欢
随机推荐
MySQL winter vacation self-study 2022 11 (9)
error Couldn‘t find a package.json file in “你的路径“
[charging station]_ Secular wisdom_ Philosophical wisdom _
Timing manager based on C #
深度学习——LSTM基础
@The problem of cross database query invalidation caused by transactional annotation
Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
Basic knowledge of tuples
laravel8 导出Excle文件
【软件逆向-分析工具】反汇编和反编译工具
MySQL winter vacation self-study 2022 11 (10)
面试字节,过关斩将直接干到 3 面,结果找了个架构师来吊打我?
优先使用对象组合,而不是类继承
Smart pointer shared_ PTR and weak_ Difference of PTR
speed or tempo in classical music
Special Edition: spreadjs v15.1 vs spreadjs v15.0
How to define a unified response object gracefully
企业级:Spire.Office for .NET:Platinum|7.7.x
Blue Bridge Cup single chip microcomputer -- PWM pulse width modulation
How about programmers' eyesight| Daily anecdotes




![[wp][入门]刷弱类型题目](/img/d0/9eb3ade701057837d98e4a20082a10.png)




