当前位置:网站首页>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;
}边栏推荐
- Research Report on competitive strategy Outlook Analysis and investment strategic planning of China's smart home equipment industry, 2022-2028
- 【RT-Thread】nxp rt10xx 设备驱动框架之--rtc搭建和使用
- [UE4] brush Arctic pack high quality Arctic terrain pack
- Market demand survey and marketing strategy analysis report of global and Chinese pet milk substitutes 2022-2028
- ArrayList分析3 : 删除元素
- Remote office tools sharing | community essay solicitation
- Where is the monitoring page of RDS database?
- The difference between i++ and ++i: tell their differences easily
- Interviewer: why is the value nil not equal to nil?
- 1146_ SiCp learning notes_ exponentiation
猜你喜欢

Wechat applet for the first time

【RT-Thread】nxp rt10xx 设备驱动框架之--Audio搭建和使用

Getting started with deops

Global and Chinese health care OEM and ODM market status survey and investment planning recommendations report 2022-2028

Kubernetes resource object introduction and common commands (III)

微服务组件Sentinel控制台调用

Collection of the most beautiful graduation photos in the graduation season, collection of excellent graduation photos

kubernetes资源对象介绍及常用命令(三)

STM32 realizes 74HC595 control

Leetcode 108 converts an ordered array into a binary search tree -- recursive method
随机推荐
[RT thread] NXP rt10xx device driver framework -- Audio construction and use
i++与++i的区别:通俗易懂的讲述他们的区别
Applet setting multi account debugging
Examination questions for the assignment of selected readings of British and American Literature in the course examination of Fujian Normal University in February 2022
Electronic Science and technology 20th autumn "Microcomputer Principle and application" online assignment 2 [standard answer]
What is the difference between cloud server and cloud virtual machine
The difference between get and post
SWM32系列教程4-端口映射及串口应用
The gbase 8A database does not support the DB2 function value (column_name, 0) cluster syntax
Servlet specification Part II
Remote office tools sharing | community essay solicitation
[combinatorics] recursive equation (special solution form | special solution solving method | special solution example)
Research Report on market demand and investment planning for the development of China's office chair industry, 2022-2028
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
[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
The third day of writing C language by Yabo people
Internet Hospital his Management Platform source, online Inquiry, appointment Registration Smart Hospital Small program source
Web-ui automated testing - the most complete element positioning method
Kubernetes resource object introduction and common commands (4)