洛谷 P1983 车站分级 拓扑排序
Code:
#include<cstdio> #include<queue> #include<algorithm> #include<cstring> using namespace std; const int N=1000+1; const int INF=10000+233; queue<int>Q; int A[N],ok[N],vis[N]; int G[N][N],d[N],degree[N]; int main() { int n,m; scanf("%d%d",&n,&m); for(int cas=1;cas<=m;++cas) { memset(ok,0,sizeof(ok)); int num; scanf("%d",&num); for(int i=1;i<=num;++i) { scanf("%d",&A[i]); ok[A[i]]=1; } A[num+1]=INF; for(int i=A[1]+1;i<A[num];++i) { if(ok[i]==0) { for(int j=1;j<=num;++j) { if(G[i][A[j]]==0) { vis[i]=1; ++degree[A[j]]; G[i][A[j]]=1; } } } } } for(int i=1;i<=n;++i)if(degree[i]==0&&vis[i]==1){Q.push(i);d[i]=1;} int ans=0; while(!Q.empty()) { int u=Q.front();Q.pop(); for(int i=1;i<=n;++i) { if(G[u][i]==1) { --degree[i]; if(degree[i]==0) { Q.push(i); } d[i]=d[u]+1; ans=max(d[i],ans); } } } printf("%d",ans); return 0; }