Dzy Loves topological sorting (BC #35 hdu 5195 topsort+ priority queue)

Source: Internet
Author: User

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


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 (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. ( testCas e ≤ 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 " span class= "Mo" id= "mathjax-span-69" style= "" >≠ ≤ ≤ ) , 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 5189

Test instructions: N points m Bar has a forward-and-no-loop graph, can delete up to the K-bar so that his topological sequence is the largest. Outputs the maximum topological order.

Idea: In the previous topsort is entered into the zero point into the queue, there are k chance to delete the edge, then I put all the points into the <=k queue, with the priority queue to maintain the maximum point sequence number, remove the point maximum sequence number of all into the edge, it added to the topological sequence, so greedy is optimal.

13278437 20 15-03-29 09:39:39 accepted 5195 374MS 8344K 2822 B C + + wust_ Lyf
13278469 2015-03-29 09:42:51 Accepted 5195 1996MS 10928K 2822 B g++ Wust_lyf

g++ and C + + differ so much


Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MAXN 200005#define MAXN 2005# Define mod 1000000009#define INF 0x3f3f3f3f#define pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid#define RSO  N rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A, I <= b; i++) #define FREE (i,a,b) for (i = A; I >= b; i--) #define FRL (i,a,b) for (i = A; I < b; i++) #define FRLL (i,a,b) for (i = A; i > b; i--) #define MEM (T, v) memset ((t), V, si Zeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF printf#define DBG pf ("hi\n") typedef long Long ll;using Nam    Espace std;struct node{int x;    int id; Friend bool operator< (const node A,const node B) {return a.id<b.id; }};int n,m,k;vector <int> EDGE[MAXN];p riority_queue<node> q;int indegree[maxn];int ans[maxn];int VIS[MAXN    ];int Main () {int i;            while (scanf ("%d%d%d", &n,&m,&k)!=eof) {for (i=0;i<=n+2;i++) {edge[i].clear ();            indegree[i]=0;  vis[i]=0;        Not ans[i]=0 in the queue;            } for (i=0;i<m;i++) {int A, B;            scanf ("%d%d", &a,&b);            indegree[b]++;        Edge[a].push_back (b); } while (!        Q.empty ()) Q.pop ();        Node node;                for (i=1;i<=n;i++) if (indegree[i]<=k) {node.id=i;                Node.x=indegree[i];                Q.push (node);        vis[i]=1;//in queue} int t=0; while (!            Q.empty ()) {Node now=q.top ();            Q.pop (); if (indegree[nOW.ID]&LT;=K) k-=indegree[now.id];                else {vis[now.id]=0;            Continue            } vis[now.id]=2;//has output ans[t++]=now.id;                For (I=0;i<edge[now.id].size (); i++) {int v=edge[now.id][i];                indegree[v]--;                Node.id=v;                NODE.X=INDEGREE[V];                    if (indegree[v]<=k && vis[v]==0) {Q.push (node);                Vis[v]=1;        }}} pf ("%d", ans[0]);        for (int i=1;i<t;i++) PF ("%d", ans[i]);    PF ("\ n"); } return 0;} /*6 6 25 61 24 52 43 42 33 2 01 21 3*/


Dzy Loves topological sorting (BC #35 hdu 5195 topsort+ 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.