题意:

  删去K条边,使拓扑排序后序列字典序最大

分析:

  因为我们要求最后的拓扑序列字典序最大,所以一定要贪心地将标号越大的点越早入队。我们定义点i的入度为di。

  假设当前还能删去k条边,那么我们一定会把当前还没入队的di≤k的最大的i找出来,把它的di条入边都删掉,然后加入拓扑序列。

  删除的一定是小连大的边,因为大连小的边在拓扑序列生成的时候就去掉了

 #include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
const int MAXN = ;
int n, m, k, u, v;
vector<int> g[MAXN];
int c[MAXN], vis[MAXN], ans[MAXN];
priority_queue<int> s;
int main()
{
while (~scanf("%d%d%d", &n, &m, &k))
{
while (!s.empty()) s.pop();
for (int i = ; i <= n; i++) g[i].clear(), vis[i] = c[i] = ;
for (int i = ; i <= m; i++)
{
scanf("%d%d", &u, &v);
g[u].push_back(v);
c[v]++;
}
for (int i = ; i <= n; i++)
if (k >= c[i]) s.push(i);
int cnt = ;
while (!s.empty())
{
int x = s.top(); s.pop();
if (c[x] <= k && !vis[x] )
{
vis[x] = ;
k -= c[x], c[x] = ;
ans[cnt++] = x;
for (int i = ; i < g[x].size(); i++)
{
c[g[x][i]]--;
if ( !c[g[x][i]] ) s.push(g[x][i]);
}
}
}
for (int i = ; i < cnt-; i++) printf("%d ", ans[i]);
printf("%d\n", ans[cnt-]);
}
}
												

HDU 5195 - DZY Loves Topological Sorting的更多相关文章

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

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

  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(topo排序 && 贪心)

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

  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 拓扑排序

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

  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 5646 DZY Loves Partition

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

  9. HDU 5646 DZY Loves Partition 数学 二分

    DZY Loves Partition 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5646 Description DZY loves parti ...

随机推荐

  1. jq改变DIV中图片的路径

    <script src="common/jquery.js"></script>  <script language="javascript ...

  2. python 中变量的命名规范

    出自:http://www.diybl.com/course/3_program/python/20111130/563643.html 模块名: 小写字母,单词之间用_分割 ad_stats.py ...

  3. [SQL注入1]From SQL injection to Shell

    第一次写,希望大神们多指点. 对于刚接触WEB渗透测试这块的朋友们,很希望能有个平台可以练习.网络上有不少,十大渗透测试演练系统,我这里推荐一个在10以外,适合初学者一步一步进步的平台PENTESTE ...

  4. activiti 5.17 流程图中文乱码问题

    1. 流程图中任务中的中文乱码显示问题.   解决方法:设置processEngineConfiguration中的两个字体属性,例如: <bean id="processEngine ...

  5. Bash的几个知识点

    1. 区别 builtin command, external command,bash script. 用builtin command(hash.type.command),而不是which命令( ...

  6. 如何在KEIL中编写模块化的C程序

    在KEIL中的模块化程序写法在使用KEIL的时候,我们习惯上在一个.c的文件中把自己要写的东西按照自己思路的顺序进行顺序书写.这样是很普遍的写法,当程序比较短的时候比如几十行或者一百多行,是没有什么问 ...

  7. KEIL 伪指令

    //为了大家查找方便,命令按字母排序:0.ALTNAME 功能: 这一伪指令用来自定义名字,以替换源程序中原来的保留字,替换的保留字均可等效地用于子程序中. 格式: ALTNAME 保留字 自定义名 ...

  8. Linux 让进程在后台可靠运行的几种方法

    我们经常会碰到这样的问题,用 telnet/ssh 登录了远程的 Linux 服务器,运行了一些耗时较长的任务, 结果却由于网络的不稳定导致任务中途失败.如何让命令提交后不受本地关闭终端窗口/网络断开 ...

  9. linux 文件内容的复制

    #include <string.h> #include <strings.h> #include <stdio.h> #include <stdlib.h& ...

  10. Linux系统下C++开发工具-远程终端软件使用

    通过前面安装Linux系统介绍,紧接着进入搭建Linux开发环境的第二步,选择C++开发工具,以及必要的客户端软件.从而完整的搭建一个Linux下C++开发的环境,便于初学者在该环境下能够很好的学习. ...