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. 【转】Android - Binder机制

    以下几篇文章是分析binder机制里讲得还算清楚的 目录 1. Android - Binder机制 - ServiceManager 2. Android - Binder机制 - 普通servic ...

  2. MACACA===gradle下载和安装

    gradle下载地址: http://services.gradle.org/distributions/ 或者直接点击这个: http://services.gradle.org/distribut ...

  3. 获取windows 网卡GUID和ip信息

    # coding: UTF-8 import _winreg GUID=dict() num = 0 netCfgInstanceID = None hkey = _winreg.OpenKey(_w ...

  4. WdatePicker做出onchange效果

    WdatePicker({onpicking: function (dp) {if (dp.cal.getDateStr() != dp.cal.getNewDateStr()) { Func(dp. ...

  5. django的事务

    在某些时候,你可能会在视图修改两张数据表.并且想让他们同时成功或者同时失败.这就是事务的原子性(atomicity).在django中应该怎么做呢? 详细可以参考官方文档:https://yiyibo ...

  6. XAlign—自动对齐代码插件

    XAlign An amazing Xcode plugin to align regular code. It can align anything by using custom alignmen ...

  7. Django web框架之模板

    1 模板: 什么是模板? html+模板语法 模版包括在使用时会被值替换掉的 变量,和控制模版逻辑的 标签. 2 模板语法: 1 变量:{{}} 深度查询: 通过句点符号 . 过滤器 filter { ...

  8. 微信小程序实战篇-下拉刷新与加载更多

    下拉刷新 实现下拉刷新目前能想到的有两种方式 1. 调用系统的API,系统有提供下拉刷新的API接口 2. 监听scroll-view,自定义下拉刷新,还记得scroll-view里面有一个binds ...

  9. 使用php后台给自己做一个页面路由,配合ajax实现局部刷新。

    今天就要放假了,把近来囤积的小玩意儿总结整理一下. 在请求一个html页面来嵌入到当前页会有一个问题,就是跟随请求过来的html他的样式表和脚本会失效.是因为文档加载的先后顺序等问题造成的.因此,加载 ...

  10. taskeng.exe禁用

    打开控制面板. 打开管理工具. 打开任务计划程序. 双击左边的的任务计划程序库,看到MySQL,然后双击MysQL,接着看到Installer,再双击Installer,这时候想禁止可以直接禁止 右击 ...