Gym - 100801G: Graph (贪心+set+拓扑)(好题)
题意:给定一个N点M边的有向图,叫你加最多K条边,使得最小拓扑序最大.
思路:不是那么简单的题. 参照了别人的代码, 最后想通了.
贪心原则: 用两个单调队列维护, 第一个序列S1单增, 表示当前入度为0的点 ; 第二个序列S2单减,表示需要加边的点.
如果S1的最大值大于S2的最大值,则对其加边.
(通俗的说对于当前入度为0的点, 显然就是越小的点越需要加边, 但是我先不给它加边, 而是放到一个集合S2里, 然后遇到最大的点(不能被加边的点)来给S2的点加边. 然后可以想象, 越小的点, 加边得越晚 ,这样可以保证越小的点越被"藏得深". 然后最小拓扑可以达到最大
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
priority_queue<int>p;
priority_queue<int,vector<int>,greater<int> >q;
vector<int>G[maxn];
int cnt,ind[maxn];
int num,used,ans[maxn],a[maxn],b[maxn];
void del(int v){
ans[++num]=v;
for(int i=;i<G[v].size();i++) if(!--ind[G[v][i]]) q.push(G[v][i]);
if(q.empty()){
if(!p.empty()){
int c=p.top(); p.pop();
G[ans[num]].push_back(c);
a[++used]=ans[num]; b[used]=c;
del(c);
}
}
}
int main()
{
int N,M,K,u,v,i,j;
scanf("%d%d%d",&N,&M,&K);
for(i=;i<=M;i++){
scanf("%d%d",&u,&v);
G[u].push_back(v);
ind[v]++;
}
for(i=;i<=N;i++) if(!ind[i]) q.push(i);
while(!q.empty()){
if(K&&(q.size()>||(!p.empty()&&q.top()<p.top()))){
K--;
int c=q.top(); p.push(c); q.pop();
if(q.empty()){
int c=p.top(); p.pop();
G[ans[num]].push_back(c);
a[++used]=ans[num]; b[used]=c;
del(c);
}
}
else{
int c=q.top(); q.pop();
del(c);
}
}
return ;
}
Gym - 100801G: Graph (贪心+set+拓扑)(好题)的更多相关文章
- 贪心/构造/DP 杂题选做Ⅱ
由于换了台电脑,而我的贪心 & 构造能力依然很拉跨,所以决定再开一个坑( 前传: 贪心/构造/DP 杂题选做 u1s1 我预感还有Ⅲ(欸,这不是我在多项式Ⅱ中说过的原话吗) 24. P5912 ...
- 贪心/构造/DP 杂题选做Ⅲ
颓!颓!颓!(bushi 前传: 贪心/构造/DP 杂题选做 贪心/构造/DP 杂题选做Ⅱ 51. CF758E Broken Tree 讲个笑话,这道题是 11.3 模拟赛的 T2,模拟赛里那道题的 ...
- HDU 5695 ——Gym Class——————【贪心思想,拓扑排序】
Gym Class Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- Codeforces Gym 101190 NEERC 16 G. Game on Graph(博弈+拓扑)
Gennady and Georgiy are playing interesting game on a directed graph. The graph has n vertices and m ...
- 洛谷 P4437 [HNOI/AHOI2018]排列(贪心+堆,思维题)
题面传送门 开始 WA ycx 的遗产(bushi 首先可以将题目转化为图论模型:\(\forall i\) 连边 \(a_i\to i\),然后求图的一个拓扑序 \(b_1,b_2,\dots b_ ...
- Codeforces Gym 100803C Shopping 贪心
Shopping 题目连接: http://codeforces.com/gym/100803/attachments Description Your friend will enjoy shopp ...
- codeforces Gym 100187L L. Ministry of Truth 水题
L. Ministry of Truth Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/p ...
- Codeforces Gym 100342H Problem H. Hard Test 构造题,卡迪杰斯特拉
Problem H. Hard TestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...
- 51Nod 1091 线段的重叠(贪心+区间相关,板子题)
1091 线段的重叠 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 X轴上有N条线段,每条线段包括1个起点和终点.线段的重叠是这样来算的,[10 2 ...
随机推荐
- VC++ 读写注冊表,注冊文件图标关联
#include <string> #include <iostream> #include <Windows.h> #include <shlobj.h&g ...
- 爬虫入门【3】BeautifulSoup4用法简介
快速开始使用BeautifulSoup 首先创建一个我们需要解析的html文档,这里采用官方文档里面的内容: html_doc = """ <html>< ...
- 注册会计师带你用Python进行探索性风险分析(一)
https://blog.csdn.net/BF02jgtRS00XKtCx/article/details/78519378
- centos samba 服务器的配置和使用
1.安装samba服务 #yum install samba samba-client samba-swat 2.修改配置文件 #vi /etc/samba/smb.conf security = u ...
- 【python】-- 递归函数、高阶函数、嵌套函数、匿名函数
递归函数 在函数内部,可以调用其他函数.但是在一个函数在内部调用自身,这个函数被称为递归函数 def calc(n): print(n) if int(n/2) == 0: #结束符 return n ...
- Docker学习总结之docker创建私有仓库(private Repositories)
Docker 创建 Private Repositories 前言 基于GFW的缘故,国内大陆基本无法pull国外的镜像,更别说官方的index了.如果images无法pull下来,那么docker就 ...
- spring ioc和aop理解
1.IOC 表示控制反转. 简单点说就是原来的对象是在要使用之前通过在代码里通过new Something()的方式创建出来的: IOC则是由spring容器创建同一创建,在程序要使用到该对象的时候, ...
- elk示例-精简版
作者:Danbo 2016-03-09 1.Grok正则捕获 input {stdin{}} filter { grok { match => { "message" =&g ...
- 【二】MongoDB入门
下面是mongodb的一些基本概念: 文档是MongoDB中数据的基本单元,类似关系数据库中的行. 集合,是存储文档的容器,类似关系数据库中的表. MongoDB的单个实例容纳多个数据库,每个数据库都 ...
- 常用阻止ajax缓存方法集锦
HTML 通过添加meta标签 <meta http-equiv= "pragma" content= "no-cache"/> (pragma: ...