You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1014 B

#include <bits/stdc++.h>
using namespace std;
const int N = 100010, M = 2 * N, INF = 0x3f3f3f3f;
int n, m;
//链式前向星
int e[M], h[N], idx, w[M], ne[M];
void add(int a, int b, int c = 0) {
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}
int in[N], f[N];
void topsort() {
queue<int> q;
for (int i = 1; i <= n; i++)
if (!in[i]) {
q.push(i);
f[i] = 1;
}
while (q.size()) {
int u = q.front();
q.pop();
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
f[j] = max(f[j], f[u] + 1);
in[j]--;
if (in[j] == 0) q.push(j);
}
}
}
int main() {
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d%d", &a, &b);
add(a, b);
in[b]++;
}
topsort();
for (int i = 1; i <= n; i++) printf("%d\n", f[i]);
return 0;
}