当前位置:网站首页>【图论】拓扑排序
【图论】拓扑排序
2022-07-04 22:30:00 【你算哪一个bug?】
拓扑排序的概念等不赘述了。
拓扑排序的方法
描述拓扑排序的图中主要维护这几个结构,其中邻接表负责描述点与点之间边的关系。
拓扑排序的操作
代码实现
拓扑排序(Topological Sorting)_神奕的博客-CSDN博客_拓扑排序
代码上我是照着他的入门的
题目
2367 – Genealogical tree (poj.org)
模板题,构建一个图,再排序输出即可。
oj不支持C++11,得把auto换成迭代器…
#include <iostream>
#include <list>
#include <queue>
#include <vector>
using namespace std;
#define N 1000
queue<int>output;
class Graph
{
public:
Graph(int v)//顶点个数初始化,new邻接表,把入度集合全部初始化为0
{
_v = v;
_adj = new list<int>[v];//new一个有v个顶点的邻接表
_indegree.resize(v, 0);//初始化入度集合
}
~Graph()
{
delete[] _adj;
}
void addEdge(int val, int w)//值为val的点指向w
{
_adj[val].push_back(w);
_indegree[w]++;
}
bool topological_sort()
{
for (int i=1;i<_indegree.size();i++)//入队结点其实还存在一层映射
{
if (!_indegree[i])
{
_q.push(i);
}
}
int count = 0;
while (!_q.empty())
{
int v = _q.front();
_q.pop();
output.push(v);
count++;
//更新入度集合,把入度为0的点入队
list<int>::iterator it = _adj[v].begin();
for (;it!=_adj[v].end();it++)
{
_indegree[*it]--;
if (!_indegree[*it])
{
_q.push(*it);
}
}
}
if (count<_v)//图有回路
{
return false;
}
else
{
return true;
}
}
private:
int _v;//顶点个数
list<int>* _adj;//邻接表
queue<int>_q;//维护入度为0的顶点的集合
vector<int> _indegree;//记录每个顶点的入度
};
int main()
{
int n;
cin >> n;
Graph g(n+1);
for (int i = 1; i <= n; i++)
{
int x;
while (cin >> x)
{
if (x == 0)
{
break;
}
g.addEdge(i, x);
}
}
g.topological_sort();
bool flag = 1;
while (!output.empty())
{
int x = output.front();
output.pop();
if (flag)
{
cout << x;
flag = 0;
}
else
{
cout << " " << x;
}
}
return 0;
}
边栏推荐
- Redis入门完整教程:事务与Lua
- vim编辑器知识总结
- Attack and defense world misc advanced area ditf
- MYSQL架构——逻辑架构
- Lost in the lock world of MySQL
- 攻防世界 misc 进阶区 2017_Dating_in_Singapore
- A complete tutorial for getting started with redis: Pipeline
- 业务太忙,真的是没时间搞自动化理由吗?
- MD5 tool class
- SPSS installation and activation tutorial (including network disk link)
猜你喜欢
Detailed explanation of heap sort code
Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
On-off and on-off of quality system construction
Advanced area a of attack and defense world misc Masters_ good_ idea
Redis入门完整教程:事务与Lua
Redis入门完整教程:列表讲解
堆排序代码详解
Advanced area of attack and defense world misc 3-11
攻防世界 MISC 进阶区 Ditf
Tla+ introductory tutorial (1): introduction to formal methods
随机推荐
Serial port data frame
MD5 tool class
醒悟的日子,我是怎么一步一步走向软件测试的道路
Introducing QA into the software development lifecycle is the best practice that engineers should follow
【lua】int64的支持
环境加密技术解析
Google Earth engine (GEE) -- take modis/006/mcd19a2 as an example to batch download the daily mean, maximum, minimum, standard deviation, statistical analysis of variance and CSV download of daily AOD
On-off and on-off of quality system construction
Why is Dameng data called the "first share" of domestic databases?
Attack and defense world misc advanced area can_ has_ stdio?
Attack and defense world misc advanced zone 2017_ Dating_ in_ Singapore
A complete tutorial for getting started with redis: transactions and Lua
Redis入门完整教程:初识Redis
华泰证券是国家认可的券商吗?开户安不安全?
NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse
新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
Advanced area a of attack and defense world misc Masters_ good_ idea
Solana chain application crema was shut down due to hacker attacks
【室友用一局王者荣耀的时间学会了用BI报表数据处理】
Common methods in string class