题意:

  删去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. 使用PHP解压文件Unzip

    这是一个非常方便的PHP函数从.zip文件解压缩文件.它有两个参数:第一个是压缩文件的路径和第二 function unzip_file($file, $destination) { // creat ...

  2. PHP学习系列(1)——字符串处理函数(2)

    6.chunk_split() 函数把字符串分割为一连串更小的部分.本函数不改变原始字符串. 语法:chunk_split(string,length,end) 参数: string——必需.规定要分 ...

  3. rsync相关整理

    第一部分 rsync服务端配置 1.下载安装 a. yum安装.  yum install rsync    b. 下载rsync安装文件安装        #tar zxvf rsync-2.6.9 ...

  4. dedecms文章的更新时间问题 每次更改文章时间变成最新的

    dedecms 每次更改文章,更新时间这里每次改了后再来看又变成当前最新时间的了. 解决方法: 查找后台目录的 templets/article_edit.htm 这个文件. 然后打开,查找如下代码: ...

  5. C 语言链表操作例程 (待完善)

    #include<stdio.h>#include<malloc.h>#include<conio.h>#include<stdlib.h>#inclu ...

  6. 有时summary的状态和details是否有open属性有关

    用过mac的同学对这个界面一定很熟悉,因为这个界面和我们今天要说的details有很多相近的地方,首先,其有折叠效果,用户可以自己选择打开或关闭哪一个,其次,当我们直接打开的时候,默认会有几个标签是打 ...

  7. Okhttp3日志采集功能

    原文地址以示尊重:http://www.jianshu.com/p/d836271b1ae4 日志采集是一个APP必备的功能,可以方便开发人员快速定位问题,解决问题,那么我们在使用okhttp的时候应 ...

  8. js运算符的优先级

    自上向下优先级降低 运算符 描述 . [] () 字段访问.数组下标.函数调用以及表达式分组 ++ -- - ~ ! delete new typeof void 一元运算符.返回数据类型.对象创建. ...

  9. Baidu百度搜索引擎登录网站 - Blog透视镜

    Baidu百度是中国的搜索引擎,有心经营中国市场的网友,自然不能错过,不过Google谷歌已经遭中国封锁,如果你的网站是用Blogger架设的,具有blogspot.com网域的,则会无法浏览. 阅读 ...

  10. JPA中以HibernatePersistence为provider的批量插入问题

    为什么要批量插入 要插入10000条数据,如果不批量插入的话,那么我们执行的sql语句将是10000条insert insert into member (group_id, user_id, rol ...