当前位置:网站首页>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;
}
边栏推荐
- Web-ui automated testing - the most complete element positioning method
- Remote office tools sharing | community essay solicitation
- IntelliJ 2021.3 short command line when running applications
- Talk about the design and implementation logic of payment process
- Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
- [combinatorics] recursive equation (the non-homogeneous part is an exponential function and the bottom is the characteristic root | example of finding a special solution)
- Analysis report on production and marketing demand and investment forecast of China's PVC industry from 2021 to 2026
- AcWing 4489. 最长子序列
- i++与++i的区别:通俗易懂的讲述他们的区别
- 远程办公工具分享|社区征文
猜你喜欢
Hongmeng third training
Analysis report on production and marketing demand and investment forecast of China's PVC industry from 2021 to 2026
POM in idea XML graying solution
How to purchase Google colab members in China
[UE4] brush Arctic pack high quality Arctic terrain pack
IntelliJ 2021.3 short command line when running applications
[combinatorics] recursive equation (summary of the solution process of recursive equation | homogeneous | double root | non-homogeneous | characteristic root is 1 | exponential form | the bottom is th
Applet setting multi account debugging
Research on Swift
国内如何购买Google Colab会员
随机推荐
How to train mask r-cnn model with your own data
互聯網醫院HIS管理平臺源碼,在線問診,預約掛號 智慧醫院小程序源碼
Servlet specification Part II
Leetcode 108 converts an ordered array into a binary search tree -- recursive method
Introduction to SolidWorks gear design software tool geartrax
SWM32系列教程4-端口映射及串口应用
Where is the monitoring page of RDS database?
鸿蒙第四次培训
Deops入门
问题随记 —— 在 edge 上看视频会绿屏
Kubernetes resource object introduction and common commands (4)
Wechat applet for the first time
【JokerのZYNQ7020】DDS_ Compiler。
Collection of the most beautiful graduation photos in the graduation season, collection of excellent graduation photos
RDS数据库的监测页面在哪看?
鸿蒙第三次培训
ArrayList analysis 3: delete elements
Investigation on the operation prospect of the global and Chinese Anti enkephalinase market and analysis report on the investment strategy of the 14th five year plan 2022-2028
VM11289 WAService. js:2 Do not have __ e handler in component:
Kotlin的协程:上下文