hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]
DZY Loves Topological Sorting
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 221 Accepted Submission(s): 52
from vertex u
to vertex v
, u
comes before v
in the ordering.
Now, DZY has a directed acyclic graph(DAG). You should find the lexicographically largest topological ordering after erasing at most k
edges from the graph.
)
The first line, three integers n,m,k(1≤n,m≤105
,0≤k≤m)
.
Each of the next m
lines has two integers: u,v(u≠v,1≤u,v≤n)
, representing a direct edge(u→v)
.
1 2
4 5
2 4
3 4
2 3
3 2 0
1 2
1 3
1 3 2
Case 1.
Erase the edge (2->3),(4->5).
And the lexicographically largest topological ordering is (5,3,1,2,4).
Problem B - DZY Loves Topological Sorting
因为我们要求最后的拓扑序列字典序最大,所以一定要贪心地将标号越大的点越早入队。我们定义点i的入度为di。假设当前还能删去k条边,那么我们一定会把当前还没入队的di≤k的最大的i找出来,把它的di条入边都删掉,然后加入拓扑序列。可以证明,这一定是最优的。
具体实现可以用线段树维护每个位置的di,在线段树上二分可以找到当前还没入队的di≤k的最大的i。于是时间复杂度就是O((n+m)logn). 不过,这里面说的还有一点不太清楚,
转一下另一个题解:
http://blog.csdn.net/glqac/article/details/44710897
看题意以为是个拓扑排序。事实上,就是个线段树。因为最多可以删k条边, 所以就是在线段树里找入度小于等于k的最大值,那么保存个区间最小就ok了。如果右子树的区间最小小于等于k那么就往右边走,因为是要找字典序最大的。当k是0,也是这样找,就跟topological sort是一样的。然后删除一个点就在线段树那个点置最大值,再删除他的边。因为总共也就m条边不超过10^5。
总复杂度o(n+m)logn。
我的解法:
用优先队列,当前节点的入度小于k便入队列,出队列时以节点编号为优先权,如果小于等于k,则输出,反之,跳过。不过写的时候遇到几个问题:
1.节点重复入队列没事,但是,不能让已经在队列中的再入队列。故要用vis,vis=0暂时不在队列中,vis=-1,在队列中,vis=1,已经输出。
2.判断队首元素的入度是否 小于k时,要用现在的入度,而不是入队列时的入度,因为,可能在处理其它点的时候,该点的入度已经发生了变化。
总的来说,还是线段树的方法思路清晰~~
13278084 | 2015-03-29 08:49:19 | Accepted | 5195 | 561MS | 6988K | 2158 B | C++ | czy |
13278083 | 2015-03-29 08:49:04 | Time Limit Exceeded | 5195 | 2000MS | 8308K | 2158 B | G++ | czy |
#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
#include <map>
#include <algorithm>
#include <queue> #define ll long long
int const N = ;
int const M = ;
int const inf = ;
ll const mod = ; using namespace std; int n,m;
int k;
int vis[N];
vector<int> bian[N];
int r[N]; struct node
{
friend bool operator < (node n1,node n2)
{
return n1.index < n2.index;
}
int index;
int d;
}; void ini()
{
int u,v;
memset(r,,sizeof(r));
memset(vis,,sizeof(vis));
int i;
for(i=;i<=n;i++){
bian[i].clear();
}
for(i=;i<=m;i++){
scanf("%d%d",&u,&v);
r[ v ]++;
bian[ u ].push_back( v );
} } void solve()
{
node te,nt;
int i;
priority_queue<node> que;
for(i=n;i>=;i--){
if(r[ i ]<=k){
te.index=i;
te.d=r[i];
que.push(te);
vis[i]=-;
}
} int ff=;
vector<int>::iterator it;
while(que.size()>=){
te=que.top();
que.pop();
// printf(" \nu=%d r=%d k=%d\n",te.index,te.d,k);
if(r[te.index]<=k){
k-=r[te.index];
vis[te.index]=;
}
else{
vis[te.index]=;
continue;
}
if(ff==){
ff=;
printf("%d",te.index);
}
else{
printf(" %d",te.index);
}
for(it=bian[te.index].begin();it!=bian[te.index].end();it++)
{
int y=*it;
r[y]--;
if(r[y]<=k && vis[y]==){
nt.index=y;
nt.d=r[y];
que.push(nt);
vis[y]=-;
}
}
}
printf("\n");
} void out()
{ } int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
//for(int cnt=1;cnt<=T;cnt++)
//while(T--)
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
ini();
solve();
out();
}
}
hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]的更多相关文章
- hdu.5195.DZY Loves Topological Sorting(topo排序 && 贪心)
DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 ...
- hdu 5195 DZY Loves Topological Sorting 线段树+拓扑排序
DZY Loves Topological Sorting Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...
- hdu 5195 DZY Loves Topological Sorting (拓扑排序+线段树)
DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 ...
- HDU 5195 DZY Loves Topological Sorting 拓扑排序
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5195 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- HDU 5195 - DZY Loves Topological Sorting
题意: 删去K条边,使拓扑排序后序列字典序最大 分析: 因为我们要求最后的拓扑序列字典序最大,所以一定要贪心地将标号越大的点越早入队.我们定义点i的入度为di. 假设当前还能删去k条边,那么我们一定会 ...
- HDU 5228 ZCC loves straight flush( BestCoder Round #41)
题目链接:pid=5228">ZCC loves straight flush pid=5228">题面: pid=5228"> ZCC loves s ...
- HDU 5806 - NanoApe Loves Sequence Ⅱ (BestCoder Round #86)
若 [i, j] 满足, 则 [i, j+1], [i, j+2]...[i,n]均满足 故设当前区间里个数为size, 对于每个 i ,找到刚满足 size == k 的 [i, j], ans + ...
- HDU 5805 - NanoApe Loves Sequence (BestCoder Round #86)
先找相邻差值的最大,第二大,第三大 删去端点会减少一个值, 删去其余点会减少两个值,新增一个值,所以新增和现存的最大的值比较一下取最大即可 #include <iostream> #inc ...
- 2019.01.22 hdu5195 DZY Loves Topological Sorting(贪心+线段树)
传送门 题意简述:给出一张DAGDAGDAG,要求删去不超过kkk条边问最后拓扑序的最大字典序是多少. 思路:贪心帮当前不超过删边上限且权值最大的点删边,用线段树维护一下每个点的入度来支持查询即可. ...
随机推荐
- a=a+b与a+=b的区别
在一次工作中身边的一位资深的同事突然问了个a=a+b与a+=b有什么区别 此时有点尴尬了 不知道是真的不知道咧还是别有用意....今天抽点时间针对此问题做个小总结 一.性能方面 a=a+b是加法运算 ...
- 成为Android高手必须掌握的8项基本要求
[1] Android操作系统概述 1. Android系统架构. 2. Android利用设计理念. 3. Android 开源知识. 4. Android 参考网站与权威信息.[2] Androi ...
- 洛谷 U10223 Cx大帝远征埃及
题目背景 众所周知,Cx是一个宇宙大犇.Cx能文善武,一直在为大一统的实现而努力奋斗着.Cx将调用他的精锐军队,一个精锐士兵最多可以战胜十个埃及士兵.同时Cx是个爱才的人,他想要制定一份能使在占领埃及 ...
- 手把手教你免费把网站IP换成1.1.1.1/1.0.0.1
近日,Cloudflare官方发文,与APNIC官方合作打算用IP1.1.1.1推出速度更快.私密性更强的DNS Cloudflare 运行全球规模最大.速度最快的网络之一. APNIC 是一个非营利 ...
- 下拉列表事件 Dropdown iview
<Dropdown @on-click="export"> <Button icon='md-log-out'> 000l <Icon type=&q ...
- 油猴和EX-百度脚本 百度网盘下载
pansoso.com 搜网盘 油猴和EX-百度脚本.zip https://aleikeji.pipipan.com/fs/845023-331102839
- 关于js中的then(盗)
then()相关的东西包括但不限于:promise,thien.js 虽然还没彻底搞清楚这些个玩意儿,但是 现在知道了 then()是干嘛的了 最主要的,是解决了异步方法立刻返回的问题 这个特性 ...
- centos下安装nodejs的三种种方式
方法一:源码包安装 官网下载 centos下载最新版10.9 https://nodejs.org/dist/v10.9.0/node-v10.9.0-linux-x64.tar.xz mkdir / ...
- python基础一day4 元组
结果: join:返回一个字符串 列表转化为字符串 可迭代对象都可以 结果: 不报错什么也不执行 结果:
- 2. CHARACTER_SETS
2. CHARACTER_SETS CHARACTER_SETS表提供有关可用字符集的信息. 下表中的SHOW Name值对应于SHOW CHARACTER SET语句的列名. INFORMATION ...