当前位置:网站首页>Topological sorting (learning notes) introduction + judge whether there is a ring
Topological sorting (learning notes) introduction + judge whether there is a ring
2022-07-27 00:08:00 【Muxi Krystal】
A topological sort ( Learning notes )
For a directed acyclic graph (DAG) Topological sort .
Realization :
1. Select a vertex in a directed graph without a precursor and output
2. Delete the vertex and all outgoing edges from the graph
3. repeat 1、2 step , Until all vertices output
initialization :
vector<int>vec[maxn];
scanf("%d%d",&n,&m);//n A little bit ,m side
int n,m,u,v;
int InDeg[maxn];// The degree of
while(m--){
scanf("%d%d",&u,&v);
vec[u].push_back(v);
InDeg[v]++;
}
Judge whether there is a ring :
queue<int>q;
int num;// Count the number of ordered elements
bool topsort(){
for(int i=1;i<=n;i++){
if(!InDeg[i]) q.push(i);
}
while(!q.empty()){
int now=q.front();
q.pop();
num++;
for(int i=0;i<vec[now].size();i++){
if(--InDeg[vec[now][i]]==0)
q.push(vec[now][i]);
}
}
if(num==n) return true;// acyclic
else return false;// Ring
}
边栏推荐
- [interview: concurrency 26: multithreading: two-phase termination mode] volatile version
- push to origin/master was rejected 错误解决方法
- 实数范围内的求模(求余)运算:负数求余究竟怎么求
- [C language] classic recursion problem
- Complex SQL_ 01
- 18. Opening and saving file dialog box usage notes
- Complete backpack and 01 Backpack
- Part II - C language improvement_ 13. Recursive function
- Design of intelligent humidification controller based on 51 single chip microcomputer
- Chapter 3 cross domain issues
猜你喜欢

Chapter 2 develop user traffic interceptors

Dynamic memory management

文件上传到服务器

3 esp8266 nodemcu network server

数据库:MySQL基础+CRUD基本操作

Bid farewell to wide tables and achieve a new generation of Bi with DQL

Azure synapse analytics Performance Optimization Guide (3) -- optimize performance using materialized views (Part 2)

第3章 跨域问题

Push to origin/master was rejected error resolution

The difference between SQL join and related subinquiry
随机推荐
Part II - C language improvement_ 13. Recursive function
【C语言】经典的递归问题
【面试:并发篇27:多线程:犹豫模式】
ES6新特性
Upload files to OSS file server
Azure Synapse Analytics 性能优化指南(4)——使用结果集缓存优化性能
Method of realizing program startup and self startup through registry
[C language] array
JUnit、JMockit、Mockito、PowerMockito
股票开户佣金是否可以调整?手机上开户安不安全
Section 6: introduction to cmake grammar
4-4 对象生命周期
Qunar travel massive indicator data collection and storage
Bid farewell to wide tables and achieve a new generation of Bi with DQL
04 traditional synchronized lock
Method of setting QQ to blank ID
Tensorflow2.0 深度学习运行代码简单教程
NFT展示指南:如何展示你的NFT藏品
NFT display guide: how to display your NFT collection
Practice of data storage scheme in distributed system