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 ...
随机推荐
- 你好,C++(20).4.2.2 表达并列条件选择的switch语句:如果……如果……如果……
4.2.2 表达并列条件选择的switch语句:如果……如果……如果…… 在现实世界中,还有这样一类特殊的条件选择: 如果明天是晴天,我就穿T恤: 如果明天是阴天,我就穿衬衣: 如果明天是雨天,我就 ...
- 类 this指针 const成员函数
C++ Primer 第07章 类 7.1.2 Sales_data类的定义如下: #ifndef SALES_DATA_H #define SALES_DATA_H #include <st ...
- MySQL学习笔记(1) - cmd登陆和退出
1. 打开cmd,输入,回车 mysql -uroot -p -P3306 -h127. 1)-u :指定用户名,root为超级权限用户. 2)-p :输入密码,不指定时回车后会提示输入密码,并用*号 ...
- 网站UV,与IP、PV
什么是网站UV,与IP.PV在概念上的区别? UV(独立访客):即Unique Visitor,访问您网站的一台电脑客户端为一个访客.00:00-24:00内相同的客户端只被计算一次. PV(访问量) ...
- python之7-1类
面向对象的编程,其实是将对象抽象成类,然后在类中,通过init定义实例初始化函数和多个操作实例的函数. 整个类就如同一个模板,我们可以用这个模板生成众多具现实例,并赋予实例动作. py中定义类的大致格 ...
- Python正则匹配递归获得给出目录下的特定类型的文件小技巧
需求是酱的: 输入一个目录,这个目录包含检测目录的必备信息但不准确需要获得后加工一下,如给出目录:C:\Program Files\Common Files\DESIGNER,需要检测的目录是:C:\ ...
- django settings最佳配置
# encoding=utf-8 import os import socket SITE_ID = 1 # 项目的根目录 # 简化后面的操作 PROJECT_ROOT = os.path.dirna ...
- http://www.cnblogs.com/zhuli19901106/p/4356419.html
http://www.cnblogs.com/zhuli19901106/p/4356419.html
- JS获取select的值
记录一下JS获取select的value值和选项值 <select id="video_status"> <option value="1" ...
- Android Studio builde.gradle 配置说明
//声明是Android应用程序 apply plugin: 'com.android.application' android { //编译SDK版本 compileSdkVersion 21 bu ...