【模板】拓扑排序

C++版本:

拓扑排序

int TP(int x, int n) {
	queue<int>q; int res = 0;
	for (int i = 1; i <= N; i++)if (!indegree[i])q.push(i);
	while (!q.empty()) {
		int u = q.front(); q.pop();
		Ti[u] += len[u];
		res = max(res, Ti[u]);
		for (int i = head[u]; i; i = eg[i].nex) {
			indegree[eg[i].to]--;
			Ti[eg[i].to] = max(Ti[eg[i].to], Ti[u]);
			if (!indegree[eg[i].to])
				q.push(eg[i].to);
		}
	}
	return res;
}
posted @ 2020-09-13 18:33  pjhui  阅读(135)  评论(0编辑  收藏  举报