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一样 在主要的拓扑排序的基础上又添加了一个要求:编号最小的节点要尽量排在前面:在满足 ...
随机推荐
- 在 kernel 下打出 有帶參數的log。 怪異現象與解決方式。
code battery_log(BAT_LOG_CRTI, "youchihwang abc10010 xxxaaa8-2\r\n"); battery_log(BAT_LOG_ ...
- 64_g2
gettext-libs-0.19.8.1-9.fc26.x86_64.rpm 15-Mar-2017 14:15 305038 gf2x-1.1-9.fc26.i686.rpm 11-Feb-201 ...
- OpenRCT2-ext
https://github.com/RollingStar/RCT-Music-Patch https://github.com/seanfisk/rct2-game-objects https:/ ...
- C后端设计开发 - 第4章-武技-常见轮子下三路
正文 第4章-武技-常见轮子下三路 后记 如果有错误, 欢迎指正. 有好的补充, 和疑问欢迎交流, 一块提高. 在此谢谢大家了. Moonlight Shadow 纪念那个我爱的, 被我感动的女孩 ...
- 动画基础--基于Core Animation(2)
参考:https://zsisme.gitbooks.io/ios-/content/ 前面的文章动画基础--基于Core Animation(1)提到了图层的基本概念以及可动画参数几何学等知识. 本 ...
- ubuntu命令行操作mysql常用操作
登陆mysql harvey@harvey-Virtual-Machine:~/ruby/mydiary$ mysql -u root -p Enter password: Welcome to th ...
- SRM 638 Div.2
250 给一个字符串 要求从一种形式换成另一形式 class NamingConvention{ private: int a, b, c; public: int d; string toCamel ...
- java.util.ConcurrentModification并发修改异常
在运行下面这段代码时出现了并发修改异常java.util.ConcurrentModification: public static void main(String[] args) { List l ...
- xshell 如何连接服务器
https://jingyan.baidu.com/article/ab69b270b0ca3d2ca7189fdc.html 点击“新建”之后就会出现下面这样一个界面,“名称”根据自己的需求填写,“ ...
- gradle eclipse 配置
http://blog.csdn.net/caolaosanahnu/article/details/17022321 从gradle官网下载 解压,配置环境变量,gradle -v 验证 gradl ...