洛谷P4742 [Wind Festival]Running In The Sky [Tarjan缩点,DAGDP]
Running In The Sky
格式难调,题面就不放了。
分析:
一句话题意:给定一张带点权的有向图,求最长点权路径及该路径上的最大点权。
很明显的$DAGDP$,因此需要缩点,将该图重建为一张$DAG$,在每个强联通分量中记录两个变量$sum,mx$表示该强联通分量中的点权和及最大点权。然后就是$DP$了,因为不仅要求最长点权路径,还要求路径上的最大点权,所以我们可以记录状态$f[x][0]$和$f[x][1]$分别表示以$x$为终点的路径中点权和最大的路径以及该路径上的最大点权。共有两个方程:
$\begin{cases}f[y][0]=f[x][0],f[y][1]=f[x][1] & (f[x][0]>f[y][0])\\f[y][1]=max(f[y][1],f[x][1]) & (f[x][0]==f[y][0]) \end{cases}$
这样子这题就很好解决了。
Code:
//It is made by HolseLee on 26th Oct 2018
//Luogu.org P4742
#include<bits/stdc++.h>
using namespace std; const int N=2e5+, M=5e5+;
int n,m,val[N],ans0,ans1,h[N],head[N],cnte,dg[N],f[N][];
int scc[N],mx[N],sum[N],idx,dfn[N],low[N],tot;
bool ins[N];
struct Edge { int to,nxt; }edge[M],e[M];
queue<int>q; stack<int>t; inline int read()
{
char ch=getchar(); int x=; bool flag=false;
while( ch<'' || ch>'' ) {
if( ch=='-' ) flag=true; ch=getchar(); }
while( ch>='' && ch<='' ) {
x=x*+ch-''; ch=getchar(); }
return flag ? -x : x;
} inline void add_edge(int x,int y)
{
edge[++cnte].to=y;
edge[cnte].nxt=h[x];
h[x]=cnte;
} inline void add(int x,int y)
{
e[++cnte].to=y, e[cnte].nxt=head[x];
head[x]=cnte, dg[y]++;
} void tarjan(int x)
{
dfn[x]=low[x]=++idx; ins[x]=; t.push(x);
int y;
for(int i=h[x]; i; i=edge[i].nxt) {
y=edge[i].to;
if( !dfn[y] ) {
tarjan(y);
low[x]=min(low[x],low[y]);
} else if( ins[y] ) {
low[x]=min(low[x],dfn[y]);
}
}
if( dfn[x]==low[x] ) {
++tot;
do {
y=t.top(); t.pop(); ins[y]=false; scc[y]=tot;
sum[tot]+=val[y], mx[tot]=max(mx[tot],val[y]);
} while( y!=x );
}
} void rebuild()
{
cnte=;
for(int x=; x<=n; ++x)
for(int i=h[x],y; i; i=edge[i].nxt) {
y=edge[i].to;
if( scc[x]!=scc[y] ) add(scc[x],scc[y]);
}
} int main()
{
n=read(), m=read();
for(int i=; i<=n; ++i) val[i]=read();
for(int i=; i<=m; ++i) add_edge(read(), read());
for(int i=; i<=n; ++i) if( !dfn[i] ) tarjan(i);
rebuild();
for(int i=; i<=tot; ++i) if( !dg[i] ) q.push(i);
while( !q.empty() ) {
int x=q.front(); q.pop();
f[x][]+=sum[x]; f[x][]=max(f[x][],mx[x]);
for(int i=head[x],y; i; i=e[i].nxt) {
y=e[i].to;
if( !(--dg[y]) ) q.push(y);
if( f[x][]>f[y][] ) {
f[y][]=f[x][], f[y][]=f[x][];
} else if( f[y][]==f[x][] ) {
f[y][]=max(f[y][],f[x][]);
}
}
}
for(int i=; i<=tot; ++i)
if( f[i][]>ans0 ) {
ans0=f[i][], ans1=f[i][];
} else if( f[i][]==ans0 ) {
ans1=max(ans1,f[i][]);
}
printf("%d %d\n",ans0,ans1);
return ;
}
洛谷P4742 [Wind Festival]Running In The Sky [Tarjan缩点,DAGDP]的更多相关文章
- T25990 [Wind Festival]Running In The Sky
T25990 [Wind Festival]Running In The Sky 题目背景 [Night - 20:02[Night−20:02 P.M.]P.M.] 夜空真美啊--但是--快要结束了 ...
- P4742 【[Wind Festival]Running In The Sky】
相信来做这道题的人肯定都学过\(Tarjan\)缩点吧,如果没有建议先去做P3387 [模板]缩点,如果你忘了,建议也去看看 满足上面要求后,你会惊奇发现,这两道题基本一样,唯一的差别就是这道题需要记 ...
- 「洛谷3469」「POI2008」BLO-Blockade【Tarjan求割点】
题目链接 [洛谷传送门] 题解 很显然,当这个点不是割点的时候,答案是\(2*(n-1)\) 如果这个点是割点,那么答案就是两两被分开的联通分量之间求组合数. 代码 #include <bits ...
- 2019/5/13 洛谷P4742 【tarjan缩点 + 拓扑dp】
题目链接:https://www.luogu.org/problemnew/show/P4742 题目大意:给一张有向图, 每个点都有点权,第一次经过该点时,该点的点权有贡献,求这张图上一条路径(终点 ...
- 洛谷P1353 USACO 跑步 Running
题目 一道入门的dp,首先要先看懂题目要求. 容易得出状态\(dp[i][j]\)定义为i时间疲劳度为j所得到的最大距离 有两个坑点,首先疲劳到0仍然可以继续疲劳. 有第一个方程: \(dp[i][0 ...
- 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur (SCC缩点,SPFA最长路,枚举反边)
P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 In an effort to better manage the grazing patterns of hi ...
- 洛谷P2341 [HAOI2006]受欢迎的牛 (Tarjan,SCC缩点)
P2341 [HAOI2006]受欢迎的牛|[模板]强连通分量 https://www.luogu.org/problem/P2341 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就 ...
- 洛谷P2863 [USACO06JAN]The Cow Prom S (tarjan)
题目简述:一个有向图,求出这个图点数>1的强连通分量的个数. 那么就是tarjan求强联通分量的模板了. 记得要用一个数组标记节点是否在栈中. 1 #include<bits/stdc++ ...
- 洛谷 1938 [USACO09NOV]找工就业Job Hunt
洛谷 1938 [USACO09NOV]找工就业Job Hunt 题目描述 Bessie is running out of money and is searching for jobs. Far ...
随机推荐
- 踩坑记(1)——使用slf4j+logback记录日志
刚开始的jar包版本如下,因为选择jar包版本不同导致的一些坑,踩过了就记录下来: <spring.version>3.1.0.RELEASE</spring.version> ...
- NodeJS API Process全局对象
Process 全局对象,可以在代码中的任何位置访问此对象,使用process对象可以截获进程的异常.退出等事件,也可以获取进程的当前目录.环境变量.内存占用等信息,还可以执行进程退出.工作目录切换等 ...
- Python数据分析初始(一)
基础库 pandas:python的一个数据分析库(pip install pandas) pandas 是基于 NumPy 的一个 python 数据分析包,主要目的是为了 数据分析 .它提供了大量 ...
- json字符串和Json对象,以及json的基本了解
考虑到python等语言中没有更好表示json对象的方法,所以使用JavaScript来介绍json 首先是json字符串: var str1 = '{ "name": " ...
- POJ-2253 Frogger(最短路)
https://vjudge.net/problem/POJ-2253 题意 公青蛙想到母青蛙那里去,期间有许多石头,公青蛙可以通过这些石头跳过去.问至少要跳的最大距离,即所有路径上石头间的最大距离的 ...
- ZCMU 1894: Power Eggs
http://acm.zcmu.edu.cn/JudgeOnline/problem.php?id=1894 题意: 有M个鹰蛋,N层楼,鹰蛋的硬度是E,也就是说在1~E层楼扔下去不会碎,E+1层楼扔 ...
- python 玩具代码
脚本语言的第一行,目的就是指出,你想要你的这个文件中的代码用什么可执行程序去运行它,就这么简单 #!/usr/bin/python是告诉操作系统执行这个脚本的时候,调用/usr/bin下的python ...
- html5 canvas高级贝塞尔曲线运动动画(好吧这一篇被批的体无完肤!都说看不懂了!没办法加注释了!当然数学不好的我也没办法了,当然这还涉及到一门叫做计算机图形学的学科)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- phpStorm 8.0.3 设置
phpstorm 8 license key Learn Programming===== LICENSE BEGIN =====63758-1204201000000Ryqh0NCC73lpRm!X ...
- 鼠标样式 cursor 全总结
本文地址:https://www.cnblogs.com/veinyin/p/10752805.html 最常用的 key pointer cursor: key; // 除了pointer, ...