kkogoro

拓扑排序模板
拓扑排序代码std::queue<int> q; for (int i = 1; i <= n;...
扫描右侧二维码阅读全文
08
2018/11

拓扑排序模板

拓扑排序

代码

std::queue<int> q;
for (int i = 1; i <= n; ++i) {
    if (indegree[i] == 0) {
        q.push(i);
    }
}
while (q.empty() == false) {
    int u = q.front();
    q.pop();
    for (int it = head[u]; it != 0; it = nextEdge[it]) {
        --indegree[to[it]];
        if (indegree[to[it]] == 0) q.push(to[it]);
    }
}

根据需求可以把q换成优先队列。

Last modification:November 8th, 2018 at 08:33 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment