题意:

  删去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. hdu2896

    数据水,但是各种wa各种t各种re最后照着别人的改了改发现了毛病 数组做指针传入的时候系统是不知道传入后的数字的长度的如果用memset他就只会讲指针的地方清零 #include<iostrea ...

  2. sql 查找数据库中某字符串所在的表及字段

    declare   @str   varchar(100)     set   @str='是否严格控制'     --要搜索的字符串         declare   @s   varchar(8 ...

  3. dedecms _ 当前位置问题的代码

    {dede:field name='position' runphp='yes'} $tc=" > "; $tf=split($tc,@me); $tn=count($tf) ...

  4. C语言基础学习学习前的准备-1

    C语言概述 欢迎来到C的世界!C语言之所以命名为C,是因为C语言源自Ken Thompson发明的B语言.它是一种可移植语言,通常一个C程序可以经过很少的改动甚至不经改动就可以在其它系统上运行:它强大 ...

  5. cygwin编译SDL1.2

    1.下载了一个SDL-1.2.14.tar.gz 2.下载一个cygwin64对SDL-1.2.14.tar.gz解压 tar -zxvf SDL-1.2.14.tar.gz 在网上找的大概是需要需要 ...

  6. js 发送ajax请求(XMLHttpRequest)

    <!DOCTYPE html><html> <head> <title></title> <script type="tex ...

  7. LeetCode_Maximal Rectangle

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  8. KEIL的混合编程操作

    http://hi.baidu.com/txz01/item/21ad9d75913a7b28d7a89c12 这一篇来讲讲混合编程的问题,在网上找了一下,讲混合编程的文件章也有不少,但进行实例操作讲 ...

  9. TranslateAnimation详解

    TranslateAnimation详解 Android JDK为我们提供了4种动画效果,分别是: AlphaAnimation,RotateAnimation, ScaleAnimation, Tr ...

  10. logstash 安装zabbix插件

    <pre name="code" class="html">[root@xxyy yum.repos.d]# yum install ruby Lo ...