当前位置:网站首页>BFS - topology sort
BFS - topology sort
2022-07-03 17:45:00 【Want no boat at the bottom of the sea】
The topological sort is bfs A simple application of , The specific implementation ideas are as follows ;
First, the degree of entry is 0 Put the point in the queue , Then take out the counterpart , Update the penetration of other points with the corresponding head , If you find a new point, the penetration is 0, Then put the point in the queue , So circular , Until the queue is empty ;
it is to be noted that , A ring graph must have no topological ordering , So a simple application of topological sorting is to judge whether a graph is a ring graph .
This can be proved by contradiction ;
Suppose a ring graph has topological ordering ;
At some point in the team , remember A In ring Q The point is a queued point in this ring , It indicates that the penetration at this point is 0, Because Q Point is the first to join the team , That points to it in the ring P I'm not in the team ,P Instructions for not joining the team Q The penetration of the point >=1, Conflict with the conditions for joining the team and the first one to join the team ;
Therefore, there is no topological ordering in a ring graph ;
Next is the code and comments .
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int N = 100010;
struct poi {
vector<int> ne;//ne Store the subscript of the next point in
int d = 0;// The penetration of the point
}pos[N];
int p[N], fur;// Topological sorting of storage points
int n, m;//n A little bit ,m side
bool bfs() {
queue<int> que;
for (int i = 1; i <= n; i++) {// Initialize queue , Set all entries to 0 Put the point in the queue
if (!pos[i].d)
que.push(i);
}
while (que.size()) {
int t = que.front();
p[fur++] = t;// Add the header to the topology sorting
que.pop();
for (int i = 0; i < pos[t].ne.size(); i++) {// Depth of update point
int m = pos[t].ne[i];
pos[m].d--;
if (!pos[m].d) {
que.push(m);
}
}
}
if (fur != n) return 0;//fur Is the number of points in the sort , If a little is not added to the sorting , It shows that there is a ring , That is, there is no topological ordering
else return 1;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {// Receiving diagram
int a, b; scanf("%d%d", &a, &b);
pos[a].ne.push_back(b); pos[b].d++;
}
if (bfs()) {
for (int i = 0; i < n; i++)
cout << p[i] << ' ';
}
else cout << -1;
}
边栏推荐
- 2021 ICPC regional competition (Shanghai) g.edge groups (tree DP)
- AcWing 3438. 数制转换
- [combinatorics] recursive equation (case where the non-homogeneous part is exponential | example where the non-homogeneous part is exponential)
- RDS数据库的监测页面在哪看?
- Loop through JSON object list
- PUT vs. POST for Uploading Files - RESTful API to be Built Using Zend Framework
- The difference between i++ and ++i: tell their differences easily
- Create a new file from templates with bash script - create new file from templates with bash script
- Research Report on investment trends and development planning of China's thermal insulation material industry, 2022-2028
- 【JokerのZYNQ7020】DDS_ Compiler。
猜你喜欢
鸿蒙第三次培训
Kubernetes resource object introduction and common commands (III)
Wechat applet for the first time
Analysis report on production and marketing demand and investment forecast of China's PVC industry from 2021 to 2026
Records of long objects and long judgments in the stream of list
Vs2013 has blocked the installer, and ie10 needs to be installed
Internet hospital his management platform source code, online consultation, appointment registration smart hospital applet source code
[set theory] order relation: summary (partial order relation | partial order set | comparable | strictly less than | covering | hasto | total order relation | quasi order relation | partial order rela
1164 Good in C
1146_ SiCp learning notes_ exponentiation
随机推荐
Y is always discrete and can't understand, how to solve it? Answer: read it several times
一入“远程”终不悔,几人欢喜几人愁。| 社区征文
聊聊支付流程的設計與實現邏輯
IntelliJ 2021.3 short command line when running applications
The third day of writing C language by Yabo people
Leetcode540: a single element in an ordered array
Stm32h7 Hal library SPI DMA transmission has been in busy solution
ES6类的继承
[combinatorics] generating function (linear property | product property)
The difference between i++ and ++i: tell their differences easily
QT学习日记9——对话框
QT learning diary 9 - dialog box
Vs2013 has blocked the installer, and ie10 needs to be installed
Golang unit test, mock test and benchmark test
[mathematical logic] equivalent calculus and reasoning calculus of predicate logic (individual word | predicate | quantifier | predicate logic formula | two basic formulas | proposition symbolization
Research on Swift
SQL injection database operation foundation
Implementation of Tetris in C language
1147_ Makefile learning_ Target files and dependent files in makefile
Qt调节Win屏幕亮度和声音大小