80分求助
# include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 7;
queue <int> q;
int head[MAX];
struct Edge {
int to;
int next;
} edge[MAX];
int tot = 0;
void adde(int u, int v) {
tot++;
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot;
}
int n, m;
int ans[MAX], ind[MAX];
void bfs() {
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = head[u]; i ; i = edge[i].next) {
int v = edge[i].to;
ans[v] = ans[u] + 1;
ind[v]--;
if(ind[v] == 0) q.push(v);
}
}
}
int main() {
cin>>n>>m;
for(int i = 1; i <= m; i++) {
int u, v;
cin>>u>>v;
adde(u, v);
ind[v]++;
}
for(int i = 1; i <= n; i++) {
if(ind[i] == 0) {
ans[i] = 1;
q.push(i);
}
}
bfs();
for(int i = 1; i <= n; i++) cout<<ans[i]<<endl;
}