HDU 4857 Escape (Reverse topology sort + priority queue)

Source: Internet
Author: User

( ̄▽ ̄) "

//This problem is required for the serial number output, the smaller ordinal priority output, so use the priority queue//Priority queue is the highest priority pop-up value, so the last to reverse the output, is the correct outputs#include <iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstring>#include<string>#include<cstdlib>#include<vector>#include<queue>using namespaceStd;typedefLong Longll;Const intmaxn=100005; Vector<int>G[MAXN];intdegree[maxn],l[maxn],n,m;voidToposort () {inttot=0; Priority_queue<int> que;//the implementation of Toposort is similar to BFS     for(intI=1; i<=n;i++)        if(!Degree[i]) Que.push (i);  while(!Que.empty ()) {        intx=que.top (); Que.pop (); L[tot++]=x;  for(intj=0; J<g[x].size (); j + +)        {            intt=G[x][j]; Degree[t]--; if(!degree[t]) Que.push (t); }    }   //if (tot==n) return true; //return false;}intMain () {intt,a,b; scanf ("%d",&t);  while(t--) {scanf ("%d%d",&n,&m); memset (degree,0,sizeof(degree)); memset (L,0,sizeof(L)); Memset (g,0,sizeof(g));  for(intI=0; i<m;i++) {scanf ("%d%d",&a,&b);            G[b].push_back (a); Degree[a]++;        } toposort ();  for(inti=n-1; i>=0; i--) {printf ("%d", L[i]); if(i!=0) printf (" "); Elseprintf ("\ n"); }    }}

HDU 4857 Escape (Reverse topology sort + priority queue)

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.