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

 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int M = ;
struct Edge
{
int v , nxt ;
Edge () {}
Edge (int v , int nxt) : v (v) , nxt (nxt) {}
}e[M];
int H[M] , E ;
bool vis[M] ;
int ans[M] , top ;
int in[M] ;
int n , m , k ;
int u , v ;
inline int read () {
int ans = ; char c; bool flag = false;
while ((c = getchar()) == ' ' || c == '\n' || c == '\r');
if (c == '-') flag = true; else ans = c - '';
while ((c = getchar()) >= '' && c <= '') ans = ans * + c - '';
return ans * (flag ? - : );
} void addedge ()
{
e[E] = Edge ( v , H[u] ) ;
H[u] = E ++ ;
} void init ()
{
E = ;
top = ;
memset (H , - , sizeof(H)) ;
memset (ans , , sizeof(ans) ) ;
memset (in , , sizeof(in)) ;
memset (vis , , sizeof(vis) ) ;
} void topo ()
{
priority_queue <int> q ;
while (!q.empty ()) q.pop () ;
for (int i = ; i <= n ; i++) if (in[i] == && !vis[i]) q.push (i) ;
while ( !q.empty () ) {
int u = q.top () ;
q.pop () ;
ans[top ++] = u ;
for (int i = H[u] ; ~ i ; i = e[i].nxt) {
in[e[i].v] -- ;
if (in[e[i].v] == && !vis[e[i].v]) q.push (e[i].v) ;
}
}
} void solve ()
{
init () ;
while (m--) {
u = read () , v = read () ;
addedge () ;
in[v] ++ ;
}
priority_queue <int> q ;
for (int i = ; i <= n ; i++) if (in[i] <= k) q.push (i) ;
while ( !q.empty () ) {
u = q.top () ;
q.pop () ;
if (in[u] > k) continue ;
ans[top ++] = u ;
k -= in[u] ;
vis[u] = ;
for (int i = H[u] ; ~ i ; i = e[i].nxt) {
in[e[i].v] -- ;
if (in[e[i].v] <= k && !vis[u] ) q.push (e[i].v) ;
}
}
topo () ;
for (int i = ; i < top ; i++) {
printf ("%d%c" , ans[i] , i == top - ? '\n' : ' ') ;
}
} int main ()
{
// freopen ("a.txt" , "r" , stdin ) ;
while (~ scanf ("%d%d%d" , &n , &m , &k)) {
solve () ;
}
return ;
}

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

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

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

  1. HDU 5195 DZY Loves Topological Sorting 拓扑排序

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5195 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

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

    传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131 ...

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

    DZY Loves Topological Sorting Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...

  4. hdu 5195 DZY Loves Topological Sorting (拓扑排序+线段树)

    DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 ...

  5. HDU 5195 - DZY Loves Topological Sorting

    题意: 删去K条边,使拓扑排序后序列字典序最大 分析: 因为我们要求最后的拓扑序列字典序最大,所以一定要贪心地将标号越大的点越早入队.我们定义点i的入度为di. 假设当前还能删去k条边,那么我们一定会 ...

  6. 2019.01.22 hdu5195 DZY Loves Topological Sorting(贪心+线段树)

    传送门 题意简述:给出一张DAGDAGDAG,要求删去不超过kkk条边问最后拓扑序的最大字典序是多少. 思路:贪心帮当前不超过删边上限且权值最大的点删边,用线段树维护一下每个点的入度来支持查询即可. ...

  7. 数据结构(线段树):HDU 5649 DZY Loves Sorting

    DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Oth ...

  8. HDU 5649.DZY Loves Sorting-线段树+二分-当前第k个位置的数

    DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Oth ...

  9. HDU 5646 DZY Loves Partition

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5646 bc:http://bestcoder.hdu.edu.cn/contests/con ...

随机推荐

  1. RF源码阅读(碎片纪录)-Python积木之contextlib

    参考页面: http://docs.python.org/2/library/contextlib.html contextlib是为了配合with语句来使用的.使用起来更加简洁.本来想写一下,这位同 ...

  2. Jenkins进阶系列之——10Publish Over SSH插件

    说明:这个插件可以通过ssh连接其他Linux机器 官方说明:Publish Over SSH 安装步骤: 系统管理→管理插件→可选插件→Artifact Uploaders→Publish Over ...

  3. Android WebView使用深入浅出

    目前很多android app都内置了可以显示web页面的界面,会发现这个界面一般都是由一个叫做WebView的组件渲染出来的,学习该组件可以为你的app开发提升扩展性. 先说下WebView的一些优 ...

  4. 分页pagination实现及其应用

    1.分页jquery.page.js //分页插件 /** 2014-08-05 ch **/ (function ($) { var ms = { init: function (obj, args ...

  5. 几种Boost算法的比较(Discrete AdaBoost, Real AdaBoost, LogitBoost, Gentle Adaboost)

    关于boost算法 boost算法是基于PAC学习理论(probably approximately correct)而建立的一套集成学习算法(ensemble learning).其根本思想在于通过 ...

  6. node 大牛的blog

    node一些基本的核心包的使用  http://cnodejs.org/topic/548e53f157fd3ae46b2334fd node的基本的三种框架的比较  http://cnodejs.o ...

  7. DOM系列---进阶篇

    内容提纲: 1.DOM类型 2.DOM扩展 3.DOM操作内容 一.DOM类型 DOM基础篇中,我们了解了DOM的节点并且了解怎样查询和操作节点,而本身这些不同的节点,又有着不同的类型.   DOM类 ...

  8. jQuery理解之(一)动画与特效

    本节主要降级和学习jQuery的自动显隐,渐入渐出.飞入飞出.自定义动画等. 1.显示和隐藏hide()和show() 对于动画来说,显示和隐藏是最基本的效果之一,本节简单介绍jQuery的显示和隐藏 ...

  9. 每天一个linux命令(14):which命令

    我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索:        which  查看可执行文件的位置.       whereis 查看文件的位置.         ...

  10. 50行代码仿backbone_todos

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...