Toposort

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5638

Description

There is a directed acyclic graph with n vertices and m edges. You are allowed to delete exact k edges in such way that the lexicographically minimal topological sort of the graph is minimum possible.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains three integers n, m and k (1≤n≤100000,0≤k≤m≤200000) -- the number of vertices, the number of edges and the number of edges to delete.

For the next m lines, each line contains two integers ui and vi, which means there is a directed edge from ui to vi (1≤ui,vi≤n).

You can assume the graph is always a dag. The sum of values of n in all test cases doesn't exceed 106. The sum of values of m in all test cases doesn't exceed 2×106.

Output

For each test case, output an integer S=(∑i=1ni⋅pi) mod (109+7), where p1,p2,...,pn is the lexicographically minimal topological sort of the graph.

Sample Input

3

4 2 0

1 2

1 3

4 5 1

2 1

3 1

4 1

2 3

2 4

4 4 2

1 2

2 3

3 4

1 4

Sample Output

30

27

30

Hint

题意

给一个DAG,然后让你最多删除k条边,使得这个图的拓扑序最小。

题解:

贪心的想一想,现在我扔出来的点是一定是入度小于等于k,且编号最小的点。

这个怎么做呢?

线段树内二分,或者直接优先队列就好了。

choose what you like.

代码

#include<stdio.h>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 2e5+7;
const int mod = 1e9+7;
vector<int> E[maxn],rE[maxn];
int in[maxn];
int inq[maxn];
int vis[maxn];
priority_queue<int,vector<int>,greater<int> >Q;
void init()
{
for(int i=0;i<maxn;i++)
E[i].clear(),rE[i].clear(),in[i]=0;
memset(inq,0,sizeof(inq));
memset(vis,0,sizeof(vis));
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
for(int i=0;i<m;i++)
{
int x,y;scanf("%d%d",&x,&y);
E[x].push_back(y);
rE[y].push_back(x);
in[y]++;
}
long long Ans = 0;
for(int i = 1 ; i <= n ; ++ i)
{
if(in[i]<=k)
{
Q.push( i );
inq[i] = 1;
}
}
int num = 1;
while(!Q.empty()){
int x = Q.top() ; Q.pop(); inq[x] = 0;
if(k >= in[x]){
vis[x] = 1 , k -= in[x];
Ans=(Ans+1ll*num*x)%mod;
num=num+1;
for(int i=0;i<E[x].size();i++){
int v =E[x][i];
if(vis[v]) continue;
in[v]--;
if(in[v] <= k&&!inq[v]){
Q.push(v);
inq[v] = 1;
}
}
}
}
printf("%I64d\n",Ans);
}
}

HDU 5638 Toposort 拓扑排序 优先队列的更多相关文章

  1. hdu 5638 Toposort (拓扑排序+线段树)

    Toposort Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  2. hdu 2647 Reward(拓扑排序+优先队列)

    Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he ...

  3. HDU 4857 拓扑排序 优先队列

    n个数,已经有大小关系,现给m个约束,规定a在b之前,剩下的数要尽可能往前移.输出序列 大小关系显然使用拓扑结构,关键在于n个数本身就有大小关系,那么考虑反向建图,优先选择值最大的入度为零的点,这样得 ...

  4. 2016"百度之星" - 初赛(Astar Round2A)HDU 5695 拓扑排序+优先队列

    Gym Class Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  5. HDU-4857-逃生-反向拓扑排序+优先队列

    HDU-4857 题意就是做一个符合条件的排序,用到拓扑序列. 我一开始wa了多发,才发现有几个样例过不了,发现1->2->3...的顺序无法保证. 后来就想用并查集强连,还是wa: 后来 ...

  6. hdu 4857 逃生 拓扑排序+PQ,剥层分析

    pid=4857">hdu4857 逃生 题目是求拓扑排序,但不是依照字典序最小输出,而是要使较小的数排在最前面. 一開始的错误思路:给每一个点确定一个优先级(该点所能到达的最小的点) ...

  7. HDU.2647 Reward(拓扑排序 TopSort)

    HDU.2647 Reward(拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数.最少金钱值是888,最少的 ...

  8. HDU 5638 拓扑排序+优先队列

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5638 题意: 给你一个DAG图,删除k条边,使得能个得到字典序尽可能小的拓扑排序 题解: 把拓扑排序 ...

  9. HDU 4857 (反向拓扑排序 + 优先队列)

    题意:有N个人,M个优先级a,b表示a优先于b.而且每一个人有个编号的优先级.输出顺序. 思路来自:与PKU3687一样 在主要的拓扑排序的基础上又添加了一个要求:编号最小的节点要尽量排在前面:在满足 ...

随机推荐

  1. JSOI2018简要题解

    来自FallDream的博客,未经允许,请勿转载,谢谢. 有幸拜读到贵省的题目,题的质量还不错,而且相比zjoi可做多了,简单发一下题解吧. 还有就是,怎么markdown在博客园上的代码这么丑啊 「 ...

  2. perl_nc.pl

    #!/usr/bin/perl use strict; use IO::Socket; use IO::Select; use Getopt::Std; my %option;getopts('lp: ...

  3. redis可编译

    redis-3.0.7 可编译 redis-3.0.7.tar.gz twemproxy-master.zip keepalived-1.2.19.tar.gz openssl-1.0.1s.tar. ...

  4. 【java报错】Unknown character set index for field '224' received from server.

    在捣腾免费数据库时,使用的一个数据库提供商的服务器使用utf8mb4编码,而我的jar包还是八百年前的.然后...然后就报错了... (1) MYSQL 5.5 之前, UTF8 编码只支持1-3个字 ...

  5. aspxgridview export导出数据,把true显示成‘是’

    项目原因,数据库中的数据是‘true’还有‘false’,但是在页面上要显示为‘是否’,导出来的时候也是要显示成‘是否’ 要在web页面当中显示成‘是否’,只要在gridview的CustomColu ...

  6. [How to] UILocalNotification 的使用

    1.简介 IOS共分为本地和远程通知两种通知,本问介绍本地通知UILocalNotification的基本使用方法. 2.本地通知的实现: 步骤一:新建工程 在Xcode中新建一个single vie ...

  7. 安装sysstat出现软件包依赖问题

    需要使用Linux性能分析工具iostat 和sar等, 这就需要安装软件包sysstat 在Ubuntu 12.04下运行sudo apt-get install sysstat,出现如下问题: 开 ...

  8. jq监听ajax执行开始,出错,结束。

    $(“#msg”).ajaxComplete(function(event,request, settings){   $(this).append(“<li>请求完成.</li&g ...

  9. 看懂sh脚本

    1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在这个例子中我们使用/bin/sh来执行程序. 当编写脚本完成时 ...

  10. hdu 1252(BFS)

    Hike on a Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...