拓扑排序模板

#include <bits/stdc++.h>
using namespace std;
struct toposort {
vector<vector<int>> e;
vector<int> tp , din;
int n ;
toposort() {}
toposort(int n) {
this->n = n;
din.resize(n + 1);
e.resize(n + 1);
}
void add(int u, int v) {
e[u].push_back(v);
din[v] ++;
}
// Kahn(卡恩算法)判环
bool topo() {
queue<int> Q;
for (int i = 1; i <= n; i ++)
if (!din[i]) Q.push(i);
while (Q.size()) {
auto u = Q.front();
Q.pop();
tp.push_back(u);
for (auto v : e[u])
if (!--din[v])
Q.push(v);
}
return tp.size() == n;
}
};

本文作者:Ke_scholar

本文链接:https://www.cnblogs.com/Kescholar/p/17898093.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Ke_scholar  阅读(24)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起