这题一开始写错的原因就是搞错了,处于西边的节点的编号不一定小,不能直接dp,要先进行拓扑排序
写到一般我才发现,其实可以一边dp,一边进行dp
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;const int N = (int)2e5+5;
int e[N],ne[N],h[N],idx = 0;
int n,m;
int record[N];
vector<int> c;
int dp[N];void add(int a,int b){e[++idx] = b, ne[idx] = h[a], h[a] = idx;
}void fun(){queue<int> q;for(int i=1;i<=n;i++){if(record[i]==0) q.push(i);}while(q.size()){int u = q.front();q.pop();c.push_back(u); // 每次都加入我们的拓扑排序的序列中 for(int i=h[u];i;i=ne[i]){int to = e[i];record[to]--;if(record[to]==0){q.push(to);}// 顺便进行dpdp[to] = max(dp[to],dp[u]+1); }}
}int main(){cin >> n >> m;for(int i=1;i<=m;i++){int x,y;cin >> x >> y;add(x,y);record[y]++;} for(int i=1;i<=n;i++) dp[i] = 1;fun();for(int i=1;i<=n;i++) {cout << dp[i] << endl;}return 0;
}