一道思维神题....

我们像网络流一样加入原点S,与汇点T

用f[i]表示原点到i的最长路,用g[i]表示i到汇点的最长路

f数组与g数组都可以dp求出来的

接下来考虑如何通过这些信息来维护删除某个点的最长路

用拓扑序来删点

我们先将所有g数组加入一个集合中,

每次删点是就先将所有该点的入边的点的f[i]+g[x]从集合中删掉

然后此时集合中最大值就是删除这个点的最优解

然后再将每个点的出边i的f[x]+g[i]加入集合中

可以发现这个集合可以用堆来维护

# include<iostream>
# include<cstdio>
# include<cmath>
# include<cstring>
# include<algorithm>
# include<queue>
using namespace std;
const int mn = ;
struct Grap{
struct edge{int to,next;};
edge e[mn*];
int head[mn],edge_max;
void add(int x,int y)
{
e[++edge_max].to=y;
e[edge_max].next=head[x];
head[x]=edge_max;
}
}A,B;
int deg[mn*],n,m,a[mn*];
int f[mn*],g[mn*];
void topolpgy()
{
int tail=,head=;
for(int i=;i<=n;i++)
if(!deg[i]) a[++head]=i;
//printf("%d %d\n",tail,head);
while(tail<=head)
{
int u=a[tail];
tail++;
for(int i=A.head[u];i;i=A.e[i].next)
{
deg[A.e[i].to]--;
if(!deg[A.e[i].to]) a[++head]=A.e[i].to;
}
}
}
struct Heap{
int heap[mn*],mark[mn*],top;
void ins(int x)
{
if(mark[x])
{
--mark[x];
return ;
}
heap[++top]=x;
int t=top;
while(t>)
{
if(heap[t]>heap[t>>])
swap(heap[t],heap[t>>]),t>>=;
else
break;
}
}
void del(int x)
{
mark[x]++;
}
void Pop()
{
heap[]=heap[top--];
int t=;
while(t<=top)
{
if( t<top && heap[t+]>heap[t] )
t++;
if(heap[t]>heap[t>>])
swap(heap[t],heap[t>>]),t<<=;
else
break;
}
}
int Top()
{
while(mark[heap[]])
mark[heap[]]--,Pop();
return heap[];
}
}T;
int main()
{
int x,y;
A.edge_max=,B.edge_max=;
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
A.add(x,y),B.add(y,x);
deg[y]++;
}
topolpgy();
/*for(int i=1;i<=n;i++)
printf("%d ",a[i]);*/
int ans=,ans_len=0x3f3f3f3f;
for(int i=;i<=n;i++)
{
int u=a[i];
f[u]=max(f[u],);
for(int j=A.head[u];j;j=A.e[j].next)
f[A.e[j].to]=max(f[A.e[j].to],f[u]+);
}
for(int i=n;i>=;i--)
{
int u=a[i];
g[u]=max(g[u],);
for(int j=A.head[u];j;j=A.e[j].next)
g[u]=max(g[u],g[A.e[j].to]+);
}
/*for(int i=1;i<=n;i++)
printf("%d:%d %d\n",i,f[i],g[i]);*/
for(int i=;i<=n;i++)
T.ins(g[i]);
T.ins();
for(int i=;i<=n;i++)
{
int u=a[i];
for(int j=B.head[u];j;j=B.e[j].next)
{
T.del(f[B.e[j].to]+g[u]);
//printf("%d ",f[B.e[j].to]+g[u]);
}
//printf("\n");
T.del(g[u]);
if(T.Top()<ans_len)
{
ans_len=T.Top(),ans=u;
//printf("%d ",T.Top());
}
for(int j=A.head[u];j;j=A.e[j].next)
{
T.ins(f[u]+g[A.e[j].to]);
//printf("%d ",f[u]+g[B.e[j].to]);
}
T.ins(f[u]);
//printf("%d\n",ans_len-1);
}
printf("%d %d",ans,ans_len-);
return ;
}

BZOJ3832Rally题解的更多相关文章

  1. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  2. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  3. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  4. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  5. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  6. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  7. poj1399 hoj1037 Direct Visibility 题解 (宽搜)

    http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...

  8. 网络流n题 题解

    学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...

  9. CF100965C题解..

    求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...

随机推荐

  1. PHP数组循环遍历的四种方式

     1.使用for循环遍历数组     conut($arr);用于统计数组元素的个数.     for循环只能用于遍历,纯索引数组!!!!     如果存在关联数组,count统计时会统计两种数组的总 ...

  2. 搭建php虚拟环境

    参考网址: http://my.oschina.net/u/998304/blog/501363?p={{totalPage}} Box镜像列表: http://www.vagrantbox.es/ ...

  3. php5 常量

    <?php define("GREETING", "Welcome to w3cschool.cn!", true); echo greeting; ?& ...

  4. logo 编程

    玩了一把logo语言,好学易懂,小朋友有兴趣是个挺不错的玩意.当然也可用于一些机器人等控制 apt install ucblogo ;一个多边形 l 边长 n 边数 to sj :l :n repea ...

  5. Linux & CentOS & RHEL

    1.修改centos7的系统编码:https://blog.csdn.net/violet_echo_0908/article/details/58063555 2.windows 环境下使用ultr ...

  6. android 读取.properties文件

    因为最终是通过流文件来进行properties文件读取的,所以很自然,我们想到要将文件放入到assets文件夹或者raw文件夹中了. 例如,我们这里有一个文件——>test.properties ...

  7. Python科学计算生态圈--Pandas

  8. linux系统服务

    系统服务分类,根据其使用的方法来分,可以被分为三类 a.由init控制的服务:基本都是系统级别的服务,运行级别这一章讲的就是这一类的服务 b.由System V启动脚本启动的服务:和我们打交道最多的一 ...

  9. 【洛谷】P1567 统计天数

    P1567 统计天数 题目背景 统计天数 题目描述 炎热的夏日,KC非常的不爽.他宁可忍受北极的寒冷,也不愿忍受厦门的夏天.最近,他开始研究天气的变化.他希望用研究的结果预测未来的天气. 经历千辛万苦 ...

  10. Vue2.0史上最全入坑教程(中)—— 脚手架代码详解

    书接上文我们说道,如何利用脚手架(vue-cli)构建一个vue项目,本回书我们一起来学习分析下代码. 回顾下创建后的项目目录:   说明:在*.vue文件,template标签里写html代码,且t ...