HZOJ-636:旅行计划

#include 
#include 
#include 
using namespace std;
#define M 100000
int indeg[M + 5] = { 0 }, ans[M + 5] = { 0 };
vector> v(M + 5);
int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 0, a, b; i < m; i++) {
		cin >> a >> b;
		indeg[b] += 1;
		v[a].push_back(b);
	}
	set s;
	for (int i = 1; i <= n; i++) {
		if (indeg[i]) continue;
		s.insert(i);
	}
	while (!s.empty()) {
		int t = *s.begin();
		s.erase(s.begin());
		for (auto x : v[t]) {
			indeg[x] -= 1;
			if (indeg[x] == 0) s.insert(x);
			ans[x] = max(ans[t] + 1, ans[x]);
		}
	}
	for (int i = 1; i <= n; i++) {
		printf("%d\n", ans[i] + 1);
	}
	return 0;
}

你可能感兴趣的:(算法题,算法,c++,数据结构)