当前位置:网站首页>Acwing: topology sequence
Acwing: topology sequence
2022-06-12 13:45:00 【Disobey the law】
( A long time ago , It's not found …)
The topological sequence
Topic link – Topological sequence of digraphs
Only digraphs have topological sequences , And a directed graph does not form a ring 
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 1e5 + 5;
int e[N], ne[N], h[N], idx, que[N], n;
int Head = 0, Tail = -1, rd[N];// Team leader out of team , End up in the team , Array recording degree
void add(int x, int y)
{
e[idx] = x, ne[idx] = h[y], h[y] = idx++;
}
bool topsort()
{
for (int i = 1; i <= n; i++)
if (rd[i] == 0) {
que[++Tail] = i;
}
while (Head <= Tail)
{
for (int i = h[que[Head++]]; i != -1; i = ne[i])
{
rd[e[i]]--;
if (rd[e[i]]==0) que[++Tail] = e[i];
// I started with an array vis To determine whether the node has been accessed by segment , Prevent heavy edges , But then
// To discover , Judging the condition of rd[e[i]]==0 This ensures that the node is accessed for the first time , Even if double edge , after rd[e[i]]--, The penetration is less than 0 了 , So don't worry about multiple visits caused by heavy edges
}
}
return Tail == n - 1;// If all the nodes of a digraph are in the sequence, it means that there is no ring , Otherwise, there is a ring , Unable to generate topology sequence
}
int main()
{
int m, x, y;
memset(h, -1, sizeof h);
cin >> n >> m;
while (m--)
{
cin >> x >> y;// Subject requirements x Point to y
add(y, x);
// I defined add function add(int x,int y) Only through y find x, That is to say y Point to x
// And later in topsort In the function, I pass x To modify the in degree of the corresponding node , So here x,y Reverse the order
// Or the add Modify the function e[idx] = y, ne[idx] = h[x], h[x] = idx++; Here is the add(x,y);
rd[y]++;
}
if (topsort())
{
for (int i = 0; i <= Tail; i++)
cout << que[i] << " ";
}
else cout << -1;
return 0;
}
边栏推荐
- Return value of WaitForSingleObject
- Redis message queue repeated consumption
- 618 entered the second half of the period, apple occupied the high-end market, and the domestic mobile phones finally undercut the price competition
- Tensorrt, onnx to tensorrt in mmclas
- 【mysql进阶】查询优化原理与方案(六)
- Seekg, tellg related file operations
- [WUSTCTF2020]颜值成绩查询-1
- A method of quickly creating test window
- 播放器屏幕方向方案
- The problem of Joseph in Informatics
猜你喜欢

torch_ geometric message passing network

import torch_ Data view of geometric

MySQL 查询 limit 1000,10 和 limit 10 速度一样快吗? 深度分页如何破解

Possible solutions to problems after CodeBlocks installation
![2061: [example 1.2] trapezoidal area](/img/83/79b73ca10615c852768aba8d2a5049.jpg)
2061: [example 1.2] trapezoidal area

List of common ACM knowledge points (to be continued)

编译安装基于fastcgi模式的多虚拟主机的wordpress和discuz的LAMP架构
![[WUSTCTF2020]颜值成绩查询-1](/img/90/e4c2882357e0a1c6a80f778887e3f5.png)
[WUSTCTF2020]颜值成绩查询-1

Multi source BFS problem template (with questions)

Transmission and response of events and use cases
随机推荐
C language array and pointer
NVIDIA Jetson Nano Developer Kit 入门
Codeforces 1629 C. Mexico array - simple greed
Time processing in C language (conversion between string and timestamp)
[wustctf2020] selfie score query -1
一种快速创建测试窗口的方法
Pre research of image scanning tool
Recursion of subviews of view
Automatic Generation of Visual-Textual Presentation Layout
618进入后半段,苹果占据高端市场,国产手机终于杀价竞争
Real time software source code of COVID-19
go-zero 微服务实战系列(二、服务拆分)
Codeforces 1629 E. grid XOR - simple thinking
A method of quickly creating test window
Innovation training (x) advanced interface beautification
Application of bit operation in C language
Code debugging - print log output to file
颜色编码格式介绍
Pytorch to onnx, onnxruntime reasoning in mmclas
数据类型转换和条件控制语句