hdu.5195.DZY Loves Topological Sorting(topo排序 && 贪心)

DZY Loves Topological Sorting

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 866    Accepted Submission(s): 250

Problem Description

A 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 should find the lexicographically largest topological ordering after erasing at most k edges from the graph.

Input

The input consists several test cases. (TestCase≤5) The first line, three integers n,m,k(1≤n,m≤105,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).

Output

For each test case, output the lexicographically largest topological ordering.

Sample Input

5 5 2
1 2
4 5
2 4
3 4
2 3
3 2 0
1 2
1 3

Sample Output

5 3 1 2 4
1 3 2

Hint

Case 1.
Erase the edge (2->3),(4->5).
And the lexicographically largest topological ordering is (5,3,1,2,4).

Source

BestCoder Round #35

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<queue>
 4 using namespace std;
 5 const int M = 100005 ;
 6 struct Edge
 7 {
 8     int v , nxt ;
 9     Edge () {}
10     Edge (int v , int nxt) : v (v) , nxt (nxt) {}
11 }e[M];
12 int H[M] , E ;
13 bool vis[M] ;
14 int ans[M] , top ;
15 int in[M] ;
16 int n , m , k ;
17 int u , v ;
18 inline int read () {
19     int ans = 0; char c; bool flag = false;
20     while ((c = getchar()) == ‘ ‘ || c == ‘\n‘ || c == ‘\r‘);
21     if (c == ‘-‘) flag = true; else ans = c - ‘0‘;
22     while ((c = getchar()) >= ‘0‘ && c <= ‘9‘) ans = ans * 10 + c - ‘0‘;
23     return ans * (flag ? -1 : 1);
24 }
25
26 void addedge ()
27 {
28     e[E] = Edge ( v , H[u] ) ;
29     H[u] = E ++ ;
30 }
31
32 void init ()
33 {
34     E = 0 ;
35     top = 0 ;
36     memset (H , - 1 , sizeof(H)) ;
37     memset (ans , 0 , sizeof(ans) ) ;
38     memset (in , 0 , sizeof(in)) ;
39     memset (vis , 0 , sizeof(vis) ) ;
40 }
41
42 void topo ()
43 {
44     priority_queue <int> q ;
45     while (!q.empty ()) q.pop () ;
46     for (int i = 1 ; i <= n ; i++) if (in[i] == 0 && !vis[i]) q.push (i) ;
47     while ( !q.empty () ) {
48         int u = q.top () ;
49         q.pop () ;
50         ans[top ++] = u ;
51         for (int i = H[u] ; ~ i ; i = e[i].nxt) {
52             in[e[i].v] -- ;
53             if (in[e[i].v] == 0 && !vis[e[i].v])    q.push (e[i].v) ;
54         }
55     }
56 }
57
58 void solve ()
59 {
60     init () ;
61     while (m--) {
62         u = read () , v = read () ;
63         addedge () ;
64         in[v] ++ ;
65     }
66     priority_queue <int> q ;
67     for (int i = 1 ; i <= n ; i++)  if (in[i] <= k) q.push (i) ;
68     while ( !q.empty () ) {
69         u = q.top () ;
70         q.pop () ;
71         if (in[u] > k) continue ;
72         ans[top ++] = u ;
73         k -= in[u] ;
74         vis[u] = 1 ;
75         for (int i = H[u] ; ~ i ; i = e[i].nxt) {
76            in[e[i].v] -- ;
77            if (in[e[i].v] <= k && !vis[u] ) q.push (e[i].v) ;
78         }
79     }
80     topo () ;
81     for (int i = 0 ; i < top ; i++) {
82         printf ("%d%c" , ans[i] , i == top - 1 ? ‘\n‘ : ‘ ‘) ;
83     }
84 }
85
86 int main ()
87 {
88    // freopen ("a.txt" , "r" , stdin ) ;
89     while (~ scanf ("%d%d%d" , &n , &m , &k)) {
90         solve () ;
91     }
92     return 0 ;
93 }

用邻接表优化后,topo的时间复杂度O(n),空间复杂度也大大减少。orz。
还有快速读入。

托它们的福,这道题让我530ms过了。233333333

时间: 2024-07-31 20:15:33

hdu.5195.DZY Loves Topological Sorting(topo排序 && 贪心)的相关文章

HDU 5195 DZY Loves Topological Sorting (拓扑排序+线段树)

题目地址:HDU 5195 简直受不了了..BC第二题都开始线段树+拓扑排序了... 这题很容易想到拓扑排序过程中贪心,但是贪心容易TLE,所以需要用数据结构去维护,我用的是线段树维护.每次找入度小于等于k的编号最大的点,这样就可以保证字典序一定是最大的. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorith

HDU 5195 DZY Loves Topological Sorting 拓扑排序

题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5195 bc(中文):http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=573&pid=1002 代码: 1 #include<algorithm> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdio&

hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]

传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 221    Accepted Submission(s): 52 Problem Description A topological sort or topological ordering of a directed

hdu 5195 DZY Loves Topological Sorting 线段树+拓扑排序

DZY Loves Topological Sorting Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5195 Description A topological sort or topological ordering of a directed graph is a linear ordering of its vertices such that for ev

HDU 5195 - DZY Loves Topological Sorting

题意: 删去K条边,使拓扑排序后序列字典序最大 分析: 因为我们要求最后的拓扑序列字典序最大,所以一定要贪心地将标号越大的点越早入队.我们定义点i的入度为di. 假设当前还能删去k条边,那么我们一定会把当前还没入队的di≤k的最大的i找出来,把它的di条入边都删掉,然后加入拓扑序列. 删除的一定是小连大的边,因为大连小的边在拓扑序列生成的时候就去掉了 1 #include <iostream> 2 #include <cstdio> 3 #include <queue>

Hdoj 5195 DZY Loves Topological Sorting 【拓扑】+【线段树】

DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 922 Accepted Submission(s): 269 Problem Description A topological sort or topological ordering of a directed graph i

DZY Loves Topological Sorting (BC #35 hdu 5195 topsort+优先队列)

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 Description A topological sort or topological ordering of a directed gr

BC DZY Loves Topological Sorting

DZY Loves Topological Sorting Accepts: 112 Submissions: 586 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) 问题描述 一张有向图的拓扑序列是图中点的一个排列,满足对于图中的每条有向边(u→v) 从 u 到 v,都满足u在排列中出现在v之前. 现在,DZY有一张有向无环图(DAG).你要在最多删去k条边之后,求出字典序最大

hdu5195 DZY Loves Topological Sorting 线段树+拓扑排序

要求在一个DAG中删去不多于k条边,使得拓扑序的字典序最大. 贪心策略:每次删去入度小于res的,序号尽量大的点的入边. 需要用线段树维护区间最小值. 代码: #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<vector> using name