Topic Portal
Topological sorting board problem, simulation can be.
Code
#include <cstdio> #include <iostream> #include <cmath> #include <cstring> #include < algorithm> #include <set> #include <map>using namespace std; #define REP (i,l,r) for (register int i= (l); <= (R); ++i) #define REPDO (I,L,R) for (register int i= (l); i>= (R);-I.) #define IL inlinetypedef double db;typedef Long Long ll;//---------------------------------------const int nsz=100050,msz=200050;int n,m;struct te{int t,pr;} Edge[msz];int hd[nsz],pe=1,in[nsz];void adde (int f,int t) {edge[++pe]= (TE) {t,hd[f]};hd[f]=pe;} int que[nsz],qh=1,qt=0;int Ts[nsz],pt=0;bool Topsort () {Rep (i,1,n) if (in[i]==0) que[++qt]=i,ts[++pt]=i; while (QH<=QT) {int u=que[qh++]; for (int i=hd[u],v=edge[i].t;i;i=edge[i].pr,v=edge[i].t) {--in[v]; if (in[v]==0) ts[++pt]=v,que[++qt]=v; }} return pt==n;} int dp[nsz];void GETDP () {Rep (i,1,n) dp[i]=1; Rep (p,1,pt) {for (int i=hd[ts[p]],v=edge[i].t;i;i=edge[i].pr,v=edge[i].t) {Dp[v]=maX (dp[v],dp[ts[p]]+1); }}}int Main () {ios::sync_with_stdio (0), Cin.tie (0); cin>>n>>m; int A, B; Rep (i,1,m) Cin>>a>>b,adde (A, B), ++in[b]; Topsort (); Rep (i,1,pt) printf ("%d", ts[i]); GETDP (); Rep (i,1,n) cout<<dp[i]<< ' \ n '; return 0;} Freopen (". In", "R", stdin);//Freopen (". Out", "w", stdout);
Luogu1137 Travel Plan (topology sort)