4010: [HNOI2015] food production

Source: Internet
Author: User

Topology sort + heap.

Turn from POPOQQQ god Ben.

Reverse build to run the topology sort and then output in reverse order.

Why not come, because do not know to choose the current dish to make which vegetables first.

In reverse order to run the topology, you can also ensure that the limit is met the number of small in front.

Off-topic: I have finished the third example of the output is not correct, a look at the problem directly abandoned therapy. It turns out that a few letters are changed ....

Toposort I always like to call "soil dial" sort, probably because my spoken English is not good ...

#include <cstdio>#include<algorithm>#include<cstring>#include<queue>using namespacestd;Const intMAXN =100000+Ten;Const intMAXM =200000+Ten;p Riority_queue<int>Q;intres[maxn],cnt;intG[MAXN],V[MAXM],NEXT[MAXM],inch[Maxn],eid;intT,n,m,u;voidAddedge (intAintb) {V[eid]=b; Next[eid]=g[a]; g[a]=eid++;inch[b]++;}intMain () {scanf ("%d",&T);  while(t--) {memset (g,-1,sizeof(g)); Eid=0; scanf ("%d%d",&n,&m); CNT=0;  for(intI=1, a,b;i<=m;i++) {scanf ("%d%d",&a,&b);                    Addedge (B,a); }         for(intI=1; i<=n;i++)if(!inch[i]) q.push (i);  while(!Q.empty ()) {u=q.top (); Q.pop ();  for(intI=g[u];~i;i=Next[i]) {                inch[v[i]]--; if(!inch[V[i]]) Q.push (V[i]); } res[++cnt]=u; }        if(cnt<n) printf ("impossible!\n"); Else {             for(inti=n;i>=1; i--) printf ("%d", Res[i]); printf ("\ n"); }    }    return 0; }

4010: [HNOI2015] food production

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.