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
1 2
4 5
2 4
3 4
2 3
3 2 0
1 2
1 3
1 3 2
Case 1.
Erase the edge (2->3),(4->5).
And the lexicographically largest topological ordering is (5,3,1,2,4).
#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排序 && 贪心)的更多相关文章
- HDU 5195 DZY Loves Topological Sorting 拓扑排序
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5195 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- 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 ...
- hdu 5195 DZY Loves Topological Sorting 线段树+拓扑排序
DZY Loves Topological Sorting Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...
- hdu 5195 DZY Loves Topological Sorting (拓扑排序+线段树)
DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 ...
- HDU 5195 - DZY Loves Topological Sorting
题意: 删去K条边,使拓扑排序后序列字典序最大 分析: 因为我们要求最后的拓扑序列字典序最大,所以一定要贪心地将标号越大的点越早入队.我们定义点i的入度为di. 假设当前还能删去k条边,那么我们一定会 ...
- 2019.01.22 hdu5195 DZY Loves Topological Sorting(贪心+线段树)
传送门 题意简述:给出一张DAGDAGDAG,要求删去不超过kkk条边问最后拓扑序的最大字典序是多少. 思路:贪心帮当前不超过删边上限且权值最大的点删边,用线段树维护一下每个点的入度来支持查询即可. ...
- 数据结构(线段树):HDU 5649 DZY Loves Sorting
DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Oth ...
- HDU 5649.DZY Loves Sorting-线段树+二分-当前第k个位置的数
DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Oth ...
- HDU 5646 DZY Loves Partition
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5646 bc:http://bestcoder.hdu.edu.cn/contests/con ...
随机推荐
- SpringMVC重定向视图RedirectView小分析
目录 前言 RedirectView介绍 实例讲解 总结 前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.cnbl ...
- sql server 清空数据库表数据
--禁用外键约束 exec sp_msforeachtable 'alter table ? nocheck constraint all ' --清空数据 truncat ...
- Linq之Linq to Sql
目录 写在前面 系列文章 Linq to sql 总结 写在前面 上篇文章介绍了linq to xml的相关内容,linq to xml提供一种更便捷的创建xml树,及查询的途径.这篇文章将继续介绍l ...
- angular的编辑器tinymce
angular的插件的确挺少的, 编辑器更是少, ui-tinymce是angular-ui推荐的一款编辑器(GIT: https://github.com/angular-ui/ui-tinymce ...
- MVC——应用Ajax获取不到数据问题解答
当我们使用控制器利用Ajax获取表单数据时,调试为null,这时看看你接受表单时定义的参数名字是否为action 其实不能起这个名字的,这个名字和控制器关键字冲突了 随便换个其它名字就好了,比如我起个 ...
- Sublime Text 3 Build 3065 All System CracKed By Hmily[LCG]
Sublime Text 3 Build 3065 All System CracKed By Hmily[LCG] <ignore_js_op> 程序员文本编辑器 Sublime Tex ...
- 小结-Splay
参照陈竞潇学长的模板写的BZOJ 3188: #include<cstdio> #include<cstring> #include<algorithm> #def ...
- Java sun的JDK
JDK概述 JDK(Java Development Kit)是Sun Microsystems针对Java开发员的产品.自从Java推出以来,JDK已经成为使用最广泛的Java SDK(Softwa ...
- Ext comboBox的remote和local的区别
remote模式下不能使用模糊查询的功能 而local模式下可以实现模糊查询的功能 如果非要实现模糊查询的功能,最好就是提前把数据查询出来,缓存到本地,然后再用local模式 且,改个属性,改成可编辑 ...
- linux中防CC攻击两种实现方法(转)
CC攻击就是说攻击者利用服务器或代理服务器指向被攻击的主机,然后模仿DDOS,和伪装方法网站,这种CC主要是用来攻击页面的,导致系统性能用完而主机挂掉了,下面我们来看linux中防CC攻击方法. 什么 ...