Bestcoder Round #35 (dzy Loves topological sorting-heap + greedy)

Source: Internet
Author: User

Dzy Loves Topological sortingTime limit:4000/2000 MS (java/others) Memory limit:131072/131072 K (java/others)
Total submission (s): 323 Accepted submission (s): 86


Problem Descriptiona topological sort or topological ordering of a directed graph is a linear ordering of its vertices suc h that for every directed edge ( Span id= "mathjax-span-4" class= "Mi" style= "font-family:mathjax_math-italic-web" >u → v ) From vertex u to vertex v , u Comes before v In the ordering.
Now, the Dzy has a directed acyclic graph (DAG). Should find the lexicographically largest topological ordering after erasing at most k Edges from the graph.
Inputthe input consists several test cases. ( T esTC ase≤5 )
The first line, three integers N,m,k(1≤N,m≤ ten 5 ,0≤k≤m) .
Each of the next m Lines has integers: u,v(u≠v,1≤u,v≤N) , representing a direct edge (u→v) .
Outputfor each test case, output the lexicographically largest topological ordering.
Sample Input
5 5 21 24 52 43 42 33 2 01 21 3

Sample Output
5 3 1 2 3 2Hintcase 1.Erase The Edge (2->3), (4->5). and the lexicographically largest topological ordering is (5,3,1,2,4).

Sourcebestcoder Round #35
Recommendhujie | We have carefully selected several similar problems for you:5197 5196 5193 5192 5191

Direct heap + greedy.

The next choice is I, obviously.

When and only if I is indegree<= the remaining number of deleted Nowk, the number of the largest

So build a heap, throw the indegree<=k in, roll out the maximum number,

Worth a question is I did not calculate the complexity, and finally directly past.

Maybe Nowk down, is going to put the original heap in the element t out, but I failed to find the inverse of tle

PS: Because next is an existing function in the system, it cannot be used,


#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include < functional> #include <iostream> #include <cmath> #include <cctype> #include <ctime> #include <queue>using namespace std; #define for (I,n) for (int. i=1;i<=n;i++) #define FORK (i,k,n) for (int i=k;i<=n;i++) #define REP (I,n) for (int. i=0;i<n;i++) #define ForD (I,n) for (int. i=n;i;i--) #define REPD (i,n) for (int i=n;i>=0;i--) # Define Forp (x) for (int p=pre[x];p, p=next[p]) #define FORPITER (x) for (int &p=iter[x];p; p=next[p]) #define Lson (x< <1) #define Rson ((x<<1) +1) #define MEM (a) memset (A,0,sizeof (a)), #define MEMI (a) memset (A,127,sizeof (a)); Define Memi (a) memset (A,128,sizeof (a)), #define INF (2139062143) #define F (100000007) #define MAXN (200000+10) #define MAXM (200000+10) typedef long Long Ll;ll Mul (ll A,ll b) {return (a*b)%F;} ll Add (ll A,ll b) {return (a+b)%F;} ll Sub (ll A,ll b) {return (a-b+ (a)/f*f+f)%F; void Upd (ll &a,ll b) {a= (a%f+b%f)%F;} Priority_queue<int> q;bool b[maxn]={0};int indegree[maxn]={0};int edge[maxm],pre[maxn],next[maxm],siz=0;void Addedge (int u,int v) {edge[++siz]=v; Next[siz]=pre[u];p Re[u]=siz; }int N,m,k;int Main () {//freopen ("sorting.in", "R", stdin), while (scanf ("%d%d%d", &n,&m,&k) ==3) {while (! Q.empty ()) Q.pop (); Mem (b) Mem (Indegree) mem (Edge) mem (pre) mem (Next) siz=0; for (i,m) {int u,v;scanf ("%d%d", &u,&v); Addedge (u,v); indegree[v]++;} for (I,n) {if (indegree[i]<=k) B[i]=1,q.push (i);} int Nowk=k;int flag=0;while (!q.empty ()) {int T;while (1) {t=q.top (); Q.pop (); if (Indegree[t]>nowk) B[t]=0;else break;} nowk-=indegree[t];indegree[t]=0; Forp (t) {int v=edge[p];indegree[v]--;if (indegree[v]<=nowk&&b[v]==0) B[v]=1,q.push (v);} if (flag) printf (""); flag++; printf ("%d", t); if (flag==n) break;} Putchar (' \ n ');} return 0;}






Bestcoder Round #35 (dzy Loves topological sorting-heap + greedy)

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.