HDU 5638 Toposort 拓扑排序 优先队列
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 拓扑排序 优先队列的更多相关文章
- hdu 5638 Toposort (拓扑排序+线段树)
Toposort Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- hdu 2647 Reward(拓扑排序+优先队列)
Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he ...
- HDU 4857 拓扑排序 优先队列
n个数,已经有大小关系,现给m个约束,规定a在b之前,剩下的数要尽可能往前移.输出序列 大小关系显然使用拓扑结构,关键在于n个数本身就有大小关系,那么考虑反向建图,优先选择值最大的入度为零的点,这样得 ...
- 2016"百度之星" - 初赛(Astar Round2A)HDU 5695 拓扑排序+优先队列
Gym Class Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- HDU-4857-逃生-反向拓扑排序+优先队列
HDU-4857 题意就是做一个符合条件的排序,用到拓扑序列. 我一开始wa了多发,才发现有几个样例过不了,发现1->2->3...的顺序无法保证. 后来就想用并查集强连,还是wa: 后来 ...
- hdu 4857 逃生 拓扑排序+PQ,剥层分析
pid=4857">hdu4857 逃生 题目是求拓扑排序,但不是依照字典序最小输出,而是要使较小的数排在最前面. 一開始的错误思路:给每一个点确定一个优先级(该点所能到达的最小的点) ...
- HDU.2647 Reward(拓扑排序 TopSort)
HDU.2647 Reward(拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数.最少金钱值是888,最少的 ...
- HDU 5638 拓扑排序+优先队列
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5638 题意: 给你一个DAG图,删除k条边,使得能个得到字典序尽可能小的拓扑排序 题解: 把拓扑排序 ...
- HDU 4857 (反向拓扑排序 + 优先队列)
题意:有N个人,M个优先级a,b表示a优先于b.而且每一个人有个编号的优先级.输出顺序. 思路来自:与PKU3687一样 在主要的拓扑排序的基础上又添加了一个要求:编号最小的节点要尽量排在前面:在满足 ...
随机推荐
- Python中的subprocess模块
Subprocess干嘛用的? subprocess模块是python从2.4版本开始引入的模块.主要用来取代 一些旧的模块方法,如os.system.os.spawn*.os.popen*.comm ...
- 如何用jQuery获得radio的值
如何获得radio的值,在网上查了一下,下面总结几种解决方法,. 1.获取选中值: $('input:radio:checked').val(): $("input[type='radio' ...
- 用vue实现登录页面
vue和mui一起完成登录页面(在hbuilder编辑器) <!DOCTYPE html> <html> <head> <meta charset=" ...
- JS中类型检测方式
在js中的类型检测目前我所知道的是三种方式,分别有它们的应用场景: 1.typeof:主要用于检测基本类型. typeof undefined;//=> undefined typeof 'a' ...
- Makefile系列之二 : 命令
一.显示命令 echo “@”字符可以控制命令是否在屏幕上显示,如 @echo 正在编译XXX模块...... 输出: 正在编译XXX模块...... 如果没有“@"则输出 : echo ...
- MyEclipse部署项目报"Add Deployment". Invalid Subscription Level - Discontinuing this MyEclipse
"Add Deployment". Invalid Subscription Level - Discontinuing this MyEclipse 猜测应该是MyEclipse ...
- FineReport——笔记
1填报分页:需要在填报预览下的链接后添加:&__cutpage__=v:
- linux命令(4):vmstat命令
CPU监控如下: vmstat 2 10 //表示每隔2秒运行10次 内存监控如下: vmstat –s 监控进程及CPU.内存状态 如下: top
- [PAT] 1021 Deepest Root (25)(25 分)
1021 Deepest Root (25)(25 分)A graph which is connected and acyclic can be considered a tree. The hei ...
- AIOps-一位研发工程师的学习笔记
https://blog.csdn.net/wxm6614/article/details/80457568