当前位置:网站首页>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;
}
边栏推荐
- Anti debugging (basic principles of debugger Design & NT NP and other anti debugging principles)
- Multimedia query
- Analysis of dagger2 principle
- The new project Galaxy token just announced by coinlist is gal
- 【无标题】
- 特殊版:SpreadJS v15.1 VS SpreadJS v15.0
- Smart pointer shared_ PTR and weak_ Difference of PTR
- speed or tempo in classical music
- Interview summary: This is a comprehensive & detailed Android interview guide
- [wp]bmzclub writeup of several questions
猜你喜欢

阿里云ECS使用cloudfs4oss挂载OSS

grandMA2 onPC 3.1.2.5的DMX参数摸索
![[groovy] string (string type variable definition | character type variable definition)](/img/d8/f02e6ad760fb039873718ff7a97b0a.jpg)
[groovy] string (string type variable definition | character type variable definition)

测试开发是什么?为什么现在那么多公司都要招聘测试开发?
![[untitled]](/img/53/f67c53d4ada382ec42f97cf68d9c71.jpg)
[untitled]
![[software reverse - basic knowledge] analysis method, assembly instruction architecture](/img/97/8001db1c572495a115d32d9dd7360e.png)
[software reverse - basic knowledge] analysis method, assembly instruction architecture

v-if VS v-show 2.0

error Couldn‘t find a package.json file in “你的路径“

Web components series (VII) -- life cycle of custom components

C # use awaiter
随机推荐
Yuancosmic ecological panorama [2022 latest]
程序员的视力怎么样? | 每日趣闻
Operation flow of UE4 DMX and grandma2 onpc 3.1.2.5
How about programmers' eyesight| Daily anecdotes
Official announcement! The third cloud native programming challenge is officially launched!
Test d'automatisation de l'interface utilisateur télécharger manuellement le pilote du navigateur à partir de maintenant
函数基础学习02
Zero foundation uses paddlepaddle to build lenet-5 network
Installation of postman and postman interceptor
Necessary fonts for designers
【web审计-源码泄露】获取源码方法,利用工具
Anti debugging (basic principles of debugger Design & NT NP and other anti debugging principles)
IronXL for .NET 2022.6
企业级:Spire.Office for .NET:Platinum|7.7.x
New interesting test applet source code_ Test available
Deep learning - LSTM Foundation
[untitled]
postman和postman interceptor的安装
Clickhouse materialized view
深度学习——LSTM基础