DZY Loves Topological Sorting (BC #35 hdu 5195 topsort + priority queue ),
DZY Loves Topological Sorting
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission (s): 264 Accepted Submission (s): 63
Problem DescriptionA topological sort or topological ordering of a directed graph is a linear ordering of its vertices such that for every directed edge (U → v) From vertex U To vertex V , U Comes before V In the ordering.
Now, DZY has a directed acyclic graph (DAG). You shocould find the lexicographically largest topological ordering after erasing at most K Edges from the graph.
InputThe input consists several test cases .( TestCase ≤ 5 )
The first line, three integers N, m, k (1 ≤ n, m ≤ 10, 0 ≤ k ≤ m) .
Each of the next M Lines has two 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 41 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
A directed acyclic graph composed of n vertices and m directed edges. You can delete Up to k edges to maximize the topological order. Output the largest topological order.
Idea: In the previous topsort, the inbound queue is a zero entry point. Here there are k chances to delete the edge, so I will put all the inbound vertices <= k into the queue, use the priority queue to maintain the maximum vertex serial number, remove all the inbound edges of the vertex maximum serial number, and add it to the topological order. Therefore, greedy is the best.
13278437 |
09:39:39 |
Accepted |
5195 |
374 MS |
8344 K |
2822 B |
C ++ |
Wust_lyf |
13278469 |
09:42:51 |
Accepted |
5195 |
1996 MS |
10928 K |
2822 B |
G ++ |
Wust_lyf |
G ++ and C ++ are very different.
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 0x3f3f3f # define pi acos (-1.0) # define eps 1e-6 # define lson rt <1, l, mid # define rson rt <1 | 1, mi D + 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, sizeof (t) # define sf (n) scanf ("% d", & n) # define sff (a, B) scanf ("% d ", & a, & B) # define sfff (a, B, c) scanf ("% d", & a, & B, & c) # define pf printf # define DBG pf ("Hi \ n") typedef long ll; using Namespace std; struct Node {int x; int id; friend bool operator <(const Node a, const Node B) {return. id <B. id ;}}; int n, m, k; vector <int> edge [maxn]; priority_queue <Node> Q; int inDegree [maxn]; int ans [maxn]; int vis [maxn]; int main () {int I; while (scanf ("% d", & n, & m, & k )! = EOF) {for (I = 0; I <= n + 2; I ++) {edge [I]. clear (); inDegree [I] = 0; vis [I] = 0; // ans [I] = 0;} for (I = 0; I <m; I ++) {int a, B; scanf ("% 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 the queue} int t = 0; while (! Q. empty () {Node now = Q. top (); Q. pop (); if (inDegree [now. id] <= k) k-= inDegree [now. id]; else {vis [now. id] = 0; continue;} vis [now. id] = 2; // The 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 */