Luogu1137 Travel Plan (topological sorting)

Source: Internet
Author: User

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&LT;=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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.