HDU 5195 - DZY Loves Topological Sorting
题意:
删去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的更多相关文章
- 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 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(topo排序 && 贪心)
DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 ...
- 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 拓扑排序
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5195 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- 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 5646 DZY Loves Partition
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5646 bc:http://bestcoder.hdu.edu.cn/contests/con ...
- HDU 5646 DZY Loves Partition 数学 二分
DZY Loves Partition 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5646 Description DZY loves parti ...
随机推荐
- BZOJ 1588: Treap 模板
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 12171 Solved: 4352 Description ...
- c++中static的使用
static可以用来修饰变量,包括函数的局部变量,类的成员变量.可以用来修饰函数,包括类的成员函数,普通函数. 今天就只说说static修饰类之外的函数的情况.假设你写了一个head.h,一个a.cp ...
- [GIT] warning: LF will be replaced by CRLF问题解决方法
原文链接[http://michael-roshen.iteye.com/blog/1328142] 开发环境: 操作系统: windows xp ruby 1.9.2 rails 3.1.3 git ...
- 职员时序安排lingo求解
大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang !职员时序安排模型 题目: 一项工作一周七天都需要有人,每天所需的最少职工数为20,16,13,1 ...
- Tomcat目录下的各个文件夹的作用
1.bin:存放各种不同平台开启与关闭Tomcat的脚本文件. 2.lib:存tomcat与web应用的Jar包 3.conf:存放tomcat的配置文件 4.webapps:web应用的发布目录,包 ...
- linux中screen命令的用法
http://www.9usb.net/201002/linux-screen-mingling.html 作为linux服务器管理员,经常要使用ssh登陆到远程linux机器上做一些耗时的操作.也许 ...
- Baidu百度搜索引擎登录网站 - Blog透视镜
Baidu百度是中国的搜索引擎,有心经营中国市场的网友,自然不能错过,不过Google谷歌已经遭中国封锁,如果你的网站是用Blogger架设的,具有blogspot.com网域的,则会无法浏览. 阅读 ...
- Android中半透明Activity效果另法
Android中的Activity有没有类似于像Windows程序样的窗口式显示呢? 答案当然是有. 下图就是一个窗口式Activity的效果图: 下面就说说实现过程: 首先看看AndroidMani ...
- Qt的Script、Quick、QML的关系与总结
背景 最近在学QML,感觉也不难,就是一直以来接触 Qt 的脚本类的东西的顺序是Script.Quick1.Declarative.Quick2.QML.那么每一个都是干什么的呢,这些东西搞的我有点混 ...
- BZOJ2084: [Poi2010]Antisymmetry
2084: [Poi2010]Antisymmetry Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 187 Solved: 125[Submit] ...