当前位置:网站首页>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;
}边栏推荐
- Red Hat将在Project Atomic上运用容器负载服务器
- Software supply chain security risk pointing North for enterprise digitalization and it executives
- Prospects of world digitalization and machine intelligence in the next decade
- How to mention hot fix and cherry pick
- Flitter - sort list sort
- Development of wireless U-shaped ultrasonic electric toothbrush
- The girlfriend said: if you want to understand the three MySQL logs, I will let you heiheihei!
- Qlineedit of QT notes (74) specifies the input type
- Cesiumjs 2022 ^ source code interpretation [6] - new architecture of modelempirical
- IFLYTEK active competition summary! (12)
猜你喜欢

QQmlApplicationEngine failed to load component qrc:/main. qml:-1 No such file or directory

BeanUtils. Copyproperties() vs. mapstruct

8253A寄存器浅析

深入理解 Jetpack Compose 内核:SlotTable 系统

Vmware16 installing win11 virtual machine (the most complete step + stepping on the pit)

Prospects of world digitalization and machine intelligence in the next decade

女朋友说:你要搞懂了MySQL三大日志,我就让你嘿嘿嘿!

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

Gateway service gateway

Solve arm_ release_ ver of this libmali is ‘g2p0-01eac0‘,rk_ so_ Ver is' 4 ', libgl1 mesa dev will not be installed, and there are unsatisfied dependencies
随机推荐
HP notebook disable touchpad after mouse is inserted
Sm2246en+ SanDisk 15131
shell 同时执行多任务下载视频
VR panorama adds contrast function to make the display of differentiation effect more intuitive!
Query points in MATLAB Delaunay triangulation
C language array interception, C string by array interception method (c/s)
PS2 handle-1 "recommended collection"
leetcode 474. Ones and zeroes (medium)
Wordpress blog uses volcano engine veimagex for static resource CDN acceleration (free)
CesiumJS 2022^ 源码解读[6] - 三维模型(ModelExperimental)新架构
composer
Qt笔记(七十四)之QLineEdit指定输入类型
Schéma de transition asynchrone - générateur
[NLP] [textcnn] text classification
基金銷售行為規範及信息管理
Shell multitasking to download video at the same time
Solutions to errors in installing OpenSSL for CentOS 6.3 x64 PHP 5.2.6 extensions
35家巨头科技公司联合组成元宇宙标准论坛组织
Don't worry about whether you can be a coder if you don't learn English well. Learn it first
[PHP] self developed framework qphp, used by qphp framework