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 ...
随机推荐
- 20145208实验一 Java开发环境的熟悉
20145208实验一 Java开发环境的熟悉 使用JDK编译.运行简单的java程序 命令行下程序开发 在命令行下建立实验目录,然后创建并进入该目录后的子目录. 编译并运行一个代码 使用IDEA 编 ...
- VS2010下配置使用OpenGL的glut库
我已在我机上测试成功,机装VS2010! 在win7(windows7 ultimate SP1)下成功安装VS2010(Visual Studio 2010 ultimate x86). 下载glu ...
- Slider 滚动条 Pagination分页插件 JS Ajax 数据范围筛选 加载 翻页 笔记
入职以后的第二个任务 根据用户所选的价格范围 筛选数据 修复BUG - 筛选数据后 总数没有更新.列表显示错误.翻页加载错误 用到的一些知识点 jquery插件系列之 - Slider滑块 max ...
- FlipView For Xamarin.Form 之 IOS
之前写过两篇博文 是关于 Android 和 Windows Phone 下的 FlipView 的实现. 上上周,有个印度佬通过 GitHub 找到我, 问我有没有打算个 ios 端的,还说比较了相 ...
- Android--多选自动搜索提示
一. 效果图 常见效果,在搜素提示选中之后可以继续搜索添加,选中的词条用特殊字符分开 二. 布局代码 <MultiAutoCompleteTextView android:id="@+ ...
- xsd、wsdl生成C#类的命令行工具使用方法
1.xsd生成C#类命令 示例:xsd <xsd文件路径> /c /o:<生成CS文件目录> <其他参数> 参数说明: /c 生成为cs文件,/d 生成DataSe ...
- 第十章:Javascript子集和扩展
本章讨论javascript的集和超集,其中子集的定义大部分处于安全考虑.只有使用这门语言的一个安全的子集编写脚本,才能让代码执行的更安全.更稳定.ECMScript3标准是1999年版本的,10年后 ...
- zabbix修改密码
在我们刚刚安装好zabbix之后我们的管理用户是 Admin,密码是zabbix 所以我们肯定是要更改密码的,这个方法也适用于我们忘记管理员密码 首先我们登录mysql数据库 这里有一个zabbix库 ...
- keep_on _coding——js_good_parts
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- hihocoder #1285 智力竞赛
传送门 总结: 1.仔细读题 2.仔细分析复杂度 3.不要想当然,乱下结论 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi.小Ho还有被小Hi强拉来的小Z,准备组队 ...