BZOJ3832 [Poi2014]Rally 【拓扑序 + 堆】
题目链接
题解
神思路orz,根本不会做
设\(f[i]\)为到\(i\)的最长路,\(g[i]\)为\(i\)出发的最长路,二者可以拓扑序后\(dp\)求得
那么一条边\((u,v)\)的对应的最长链就是\(f[u] + 1 + g[v]\)
我们人为加入源汇点\(S\),\(T\),\(S\)向每个点连边,每个点向\(T\)连边
我们考虑把整个图划分开
一开始所有点都在\(T\)这边,割边为所有\(S\)的边
然后我们按照拓扑序把点逐一加入\(S\)集合中
加入时,我们删去\(S\)集合连向该点的边,然后询问所有边的最大值,即为删去该点的最长链
加入后,我们加入该点连向\(T\)集合的边
由于是按照拓扑序,所以以上提到的所有边就是该点的所有入边/出边
然后所有边的最大值可以用堆或者线段树维护
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u]; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 500005,maxm = 2000005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
struct Heap{
priority_queue<int> a,b;
void ck(){while (!b.empty() && a.top() == b.top()) a.pop(),b.pop();}
int size(){return a.size() - b.size();}
void ins(int x){ck(); a.push(x);}
void del(int x){ck(); b.push(x);}
int top(){ck(); return size() ? a.top() : 0;}
}H;
int n,m,f[maxn],g[maxn],s[maxn];
int q[maxn],head,tail;
int h[maxn],de[maxn],ne;
int hi[maxn],nei;
struct EDGE{int to,nxt;}ed[maxm],e[maxm];
inline void build(int u,int v){
ed[++ne] = (EDGE){v,h[u]}; h[u] = ne;
de[v]++;
e[++nei] = (EDGE){u,hi[v]}; hi[v] = nei;
}
void topu(){
head = 0; tail = -1; int u;
REP(i,n) if (!de[i]) q[++tail] = i;
REP(i,n){
s[i] = u = q[head++];
Redge(u) if (!(--de[ed[k].to])) q[++tail] = ed[k].to;
}
}
void init(){
REP(i,n){
int u = s[i];
Redge(u) f[ed[k].to] = max(f[ed[k].to],f[u] + 1);
}
for (int i = n; i; i--){
int u = s[i];
Redge(u) g[u] = max(g[u],g[ed[k].to] + 1);
}
}
void solve(){
int ans = INF,ansu = 0,x;
REP(i,n) H.ins(g[i]);
REP(i,n){
int u = s[i];
H.del(g[u]);
for (int k = hi[u]; k; k = e[k].nxt)
H.del(f[e[k].to] + 1 + g[u]);
x = H.top();
if (x < ans) ans = x,ansu = u;
H.ins(f[u]);
Redge(u) H.ins(f[u] + 1 + g[ed[k].to]);
}
printf("%d %d\n",ansu,ans);
}
int main(){
n = read(); m = read();
int a,b;
REP(i,m){
a = read(); b = read();
build(a,b);
}
topu();
init();
//REP(i,n) printf("node%d f = %d g = %d\n",i,f[i],g[i]);
solve();
return 0;
}
BZOJ3832 [Poi2014]Rally 【拓扑序 + 堆】的更多相关文章
- BZOJ3832: [Poi2014]Rally(拓扑排序 堆)
题意 题目链接 Sol 最直观的思路是求出删除每个点后的最长路,我们考虑这玩意儿怎么求 设\(f[i]\)表示以\(i\)结尾的最长路长度,\(g[i]\)表示以\(i\)开始的最长路长度 根据DAG ...
- 【BZOJ-3832】Rally 拓扑序 + 线段树 (神思路题!)
3832: [Poi2014]Rally Time Limit: 20 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 168 Solved: ...
- BZOJ3832[Poi2014]Rally——权值线段树+拓扑排序
题目描述 An annual bicycle rally will soon begin in Byteburg. The bikers of Byteburg are natural long di ...
- 并不对劲的bzoj3832: [Poi2014]Rally
传送门-> 这题的原理看上去很神奇. 称拓扑图中入度为0的点为“起点”,出度为0的点为“终点”. 因为“起点”和“终点”可能有很多个,算起来会很麻烦,所以新建“超级起点”S,向所有点连边,“超级 ...
- BZOJ3832 : [Poi2014]Rally
f[0][i]为i出发的最长路,f[1][i]为到i的最长路 新建源汇S,T,S向每个点连边,每个点向T连边 将所有点划分为两个集合S与T,一开始S中只有S,其它点都在T中 用一棵线段树维护所有连接属 ...
- 【BZOJ3832】[POI2014]Rally(拓扑排序,动态规划)
[BZOJ3832][POI2014]Rally(拓扑排序,动态规划) 题面 BZOJ,权限题 洛谷 题解 这题好强啊,感觉学了好多东西似的. 首先发现了一个图画的很好的博客,戳这里 然后我来补充一下 ...
- BZOJ-4010 菜肴制作 贪心+堆+(拓扑图拓扑序)
无意做到...char哥还中途强势插入干我...然后据他所言,看了一会题,一转头,我爆了正解....可怕 4010: [HNOI2015]菜肴制作 Time Limit: 5 Sec Memory L ...
- [POI2014]Rally
OJ题号:BZOJ3832.洛谷3573 思路: 建立超级源汇$S$和$T$,DP求出分别以$S$和$T$为源点的最长路$diss$和$dist$. 对于每条边$i$,设定一个权值$w_i=diss_ ...
- 3832: [Poi2014]Rally
3832: [Poi2014]Rally 链接 分析: 首先可以考虑删除掉一个点后,计算最长路. 设$f[i]$表示从起点到i的最长路,$g[i]$表示从i出发到终点的最长路.那么经过一条边的最长路就 ...
随机推荐
- PHP使用Redis消息队列
1.redis安装 参考:菜鸟教程http://www.runoob.com/redis/redis-install.html 2.安装php的redis扩展 1)phpinfo()查看php版本信息 ...
- XSS----payload,绕过,xss小游戏记录
一.XSS 1.原理:攻击者把恶意的脚本代码注入到网页中,等待其他用户浏览 这些网页(或触发其他条件),从而执行其中的恶意代码. 1.xss实例代码: test.html <!DOCTYPE h ...
- tpo-10 C1 How to get photographs exhibited
第 1 段 1.Listen to a conversation between a student and her Photography professor. 听一段学生和摄影学教授的对话. 第 ...
- smartgit 使用
合并分支
- [HNOI2017]大佬
参考题解 \(\text{Solution}\) 我们发现5个行为中2操作与其它操作无关,所以我们采用贪心,尽量让多的时间去攻击大佬. 设 \(f[i][j]\) 表示前 \(i\) 天剩 \(j\) ...
- [问题] docker: Failed to start Docker Application Container Engine.
docker无法启动: # systemctl restart docker Job for docker.service failed because the control process exi ...
- 十 Writing YARN Applications
本节介绍: 使用yarn 高级提交写yarn应用程序.其实已经yarn底层API.MR计算框架对底层的API实现了封装. 高级提交指直接使用yarn的三种接口来提交应用程序: 1)YarnCl ...
- POJ 2826 An Easy Problem?!(线段交点+简单计算)
Description It's raining outside. Farmer Johnson's bull Ben wants some rain to water his flowers. Be ...
- 常用算法Java实现之冒泡排序
冒泡排序是所有排序算法中最基本.最简单的一种.思想就是交换排序,通过比较和交换相邻的数据来达到排序的目的. 具体流程如下: 1.对要排序的数组中的数据,依次比较相邻的两个数据的大小. 2.如果前面的数 ...
- JavaScript初探系列之Ajax应用
一 什么是Ajax Ajax是(Asynchronous JavaScript And XML)是异步的JavaScript和xml.也就是异步请求更新技术.Ajax是一种对现有技术的一种新的应用,不 ...