当前位置:网站首页>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;
}边栏推荐
- Simple application example of rhai script engine
- New trends of China's national tide development in 2022
- How to distinguish between platform security and online hype? What are the stop loss techniques for online speculation?
- Pycharm is very fast to learn from installation to full armament. There are so many pictures because it is too detailed!
- 什么是SRM系统,如何规范公司内部采购流程
- 需求评审,测试人员应该发挥怎样的价值?两分钟让你不再懵逼
- Tide - rust web framework based on async STD
- leetcode 474. Ones and zeroes (medium)
- CesiumJS 2022^ 源码解读[6] - 三维模型(ModelExperimental)新架构
- 网上开华泰证券的股票账户是否安全呢?
猜你喜欢

HP notebook disable touchpad after mouse is inserted

ABAQUS 2022 software installation package and installation tutorial

206 page Shanghai BIM Technology Application and development report 2021

Design e-commerce seckill system

Why did kubernetes win? The changes in the container circle!

To tell you the truth, ThreadLocal is really not an advanced thing

一次革命、两股力量、三大环节:《工业能效提升行动计划》背后的“减碳”路线图

Bridge emqx cloud data to AWS IOT through the public network

Software supply chain security risk pointing North for enterprise digitalization and it executives
![[designmode] factory pattern](/img/62/9be808b3e1c2139d564caa307fcd30.png)
[designmode] factory pattern
随机推荐
To tell you the truth, ThreadLocal is really not an advanced thing
[NLP] [textcnn] text classification
Ctfshow framework reproduction
Thoughts on the future of data analysis in "miscellaneous talk"
Understand target detection in one article: r-cnn, fast r-cnn, fast r-cnn, Yolo, SSD "suggestions collection"
5G智慧建筑解决方案2021
网上开华泰证券的股票账户是否安全呢?
A detailed explanation of the implementation principle of go Distributed Link Tracking
[leetcode] [SQL] notes
NATs cluster deployment
基金銷售行為規範及信息管理
Wordpress blog uses volcano engine veimagex for static resource CDN acceleration (free)
Repetition is the mother of skill
五分钟搞懂探索式测试
高等数学建模
Tide - rust web framework based on async STD
未来十年世界数字化与机器智能展望
Fund managers' corporate governance and risk management
shell 同时执行多任务下载视频
Why did kubernetes win? The changes in the container circle!