Topic Links:
hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5195
BC (English): http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=573&pid=1002
Code:
1#include <algorithm>2#include <iostream>3#include <cstring>4#include <cstdio>5#include <vector>6#include <queue>7 using namespacestd;8 9 Const intMAXN = 1e5 +Ten;Ten Const intINF =0x3f3f3f3f; One A intN, M, K; - - structNode { the intv, Flag; -Node (intVintflag=0): V (v), flag (flag) {} -Node () {flag =0; } - }; + -Vector<node>HEAD[MAXN]; + intIND[MAXN],DONE[MAXN]; A at voidinit () { - for(inti =0; I < n; i++) { -Ind[i] = done[i]=0; - head[i].clear (); - } - } in - intMain () { to while(SCANF ("%d%d%d", &n, &m, &k) = =3&&N) { + init (); - for(inti =0; I < m; i++) { the intu, v; *scanf"%d%d", &u, &v); U--, v--; $ind[v]++;Panax NotoginsengHead[u].push_back (Node (V,0)); - } the +priority_queue<int>PQ; A the //Delete Edge + for(inti = n-1; I >=0; i--) { - if(k >=Ind[i]) { $ //will I in the Father UI, meet Ui<i, ie Edge (ui,i) deleted, here to note, for Edge (Ui,i), ui>i side, do not delete at all $K-=Ind[i]; - Pq.push (i); - for(intj =0; J < Head[i].size (); J + +) { theNode &nd =Head[i][j]; - if(I >nd.v) {Wuyi //Delete the Edge (i,v), the topological sort of time can not go this side theNd.flag =1; -ind[nd.v]--; Wu } - } About } $ } - -vector<int>ans; - //Topological sorting A while(!Pq.empty ()) { + intU =pq.top (); Pq.pop (); the if(Done[u])Continue; - ans.push_back (u); $Done[u] =1; the for(inti =0; I < head[u].size (); i++) { thenode& nd =Head[u][i]; the if(done[nd.v]| | Nd.flag)Continue; theind[nd.v]--; - if(IND[ND.V] = =0) Pq.push (ND.V); in } the } the Aboutprintf"%d", ans[0]+1); the for(inti =1; I < ans.size (); i++) printf ("%d", ans[i]+1); theprintf"\ n"); the } + return 0; - } the /*Bayi 5 3 1 the 4 3 the 1 3 - 3 2 - the 5 3 0 the 4 3 the 1 3 the 3 2 - */
HDU 5195 Dzy Loves topological sorting topology sequencing