date: 2024-05-09
title: Graph-Overview
status: UNFINISHED
author:
- AllenYGY
tags:
- Graph
- Overview
created: 2024-05-09T23:36
updated: 2024-06-11T01:15
publish: True
Graph-Overview
行迹一定是轨道,轨道不一定是行迹
vector<int> topo_sort(int k, vector<vector<int>> &edges) {
vector<vector<int>> g(k);
vector<int> in_deg(k);
for (auto &e : edges) {
int x = e[0], y = e[1] ; // 顶点编号从 0 开始,方便计算
g[x].push_back(y);
++in_deg[y];
}
vector<int> order;
queue<int> q;
for (int i = 0; i < k; ++i)
if (in_deg[i] == 0)
q.push(i);
while (!q.empty()) {
int x = q.front();
q.pop();
order.push_back(x);
for (int y : g[x])
if (--in_deg[y] == 0)
q.push(y);
}
return order;
}