P3387 【模板】缩点 题解 (Tarjan)
题目链接
解题思路
这几天搞图论,好有趣hhh,多写几篇博客。
上次学\(Tarjan\)求割点,这次缩点。
思路大概是多一个栈和染色的步骤,每次\(Tarjan\)的时候把点入栈,如果某个点(比较像割点但不完全是)的\(DFS\)子树都搜不到它祖宗,那么接下来进行的遍历操作必然与该点不能形成强连通分量,所以可以遇到\(low[p]==dfn[p]\)的点就把栈里面的东西全弹出来,染色表示这是一个强连通分量。
对于这个题,强连通分量之间再进行连边,记忆化搜索(类似树形DP)即可。
AC代码
#include<stdio.h>
#include<string.h>
#define N 100010
#define M 10010
#define min(a,b) (a>b?b:a)
#define max(a,b) (a<b?b:a)
int n,m;
int v[M],x[N],y[N];
struct Edge{
int end,near;
}e[N];
int head[M],cnt;
void add(int a,int b){
e[++cnt].end=b;
e[cnt].near=head[a];
head[a]=cnt;
}
int dfn[M],tot,sta[M],top,vis[M],low[M];
int color[M],num,val[M];
void tarjan(int p){
dfn[p]=low[p]=++tot;
sta[++top]=p;vis[p]=1;
int i;
for(i=head[p];i;i=e[i].near){
int q=e[i].end;
if(!dfn[q])tarjan(q),low[p]=min(low[p],low[q]);
else if(vis[q])low[p]=min(low[p],low[q]);
}
if(dfn[p]==low[p]){
num++;
while(sta[top+1]!=p){
color[sta[top]]=num;
vis[sta[top]]=0;
val[num]+=v[sta[top]];
top--;
}
}
}
int f[M];
void dp(int p){
int i;
for(i=head[p];i;i=e[i].near){
int q=e[i].end;
if(!f[q])dp(q);
f[p]=max(f[p],f[q]);
}
f[p]+=val[p];
}
int main(){
int i,ans=0;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)scanf("%d",&v[i]);
for(i=0;i<m;i++)scanf("%d%d",&x[i],&y[i]),add(x[i],y[i]);
for(i=1;i<=n;i++)
if(!dfn[i])tarjan(i);//tarjan缩点
memset(head,0,sizeof(head));cnt=0;//清空边
for(i=0;i<m;i++)//强连通分量之间连边
if(color[x[i]]^color[y[i]])add(color[x[i]],color[y[i]]);
for(i=1;i<=num;i++)if(!f[i])dp(i),ans=max(ans,f[i]);//DP
printf("%d",ans);
return 0;
}
P3387 【模板】缩点 题解 (Tarjan)的更多相关文章
- 洛谷P3387 【模板】缩点 题解
背景 今天\(loj\)挂了,于是就有了闲情雅致来刷\(luogu\) 题面 洛谷P3387 [模板]缩点传送门 题意 给定一个\(n\)个点\(m\)条边有向图,每个点有一个权值,求一条路径,使路径 ...
- 【Luogu P3387】缩点模板(强连通分量Tarjan&拓扑排序)
Luogu P3387 强连通分量的定义如下: 有向图强连通分量:在有向图G中,如果两个顶点vi,vj间(vi>vj)有一条从vi到vj的有向路径,同时还有一条从vj到vi的有向路径,则称两个顶 ...
- 【模板】缩点(Tarjan算法)/洛谷P3387
题目链接 https://www.luogu.com.cn/problem/P3387 题目大意 给定一个 \(n\) 个点 \(m\) 条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之 ...
- Tarjan+topsort(DP)【P3387】 [模板]缩点
Description 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只计算一次 ...
- 【Luogu】P3387缩点(Tarjan缩点+深搜DP)
题没什么好说的,因为是模板题.求值我用的是dfs. 不能直接在原图上dfs,因为原图上有环的话会发生一些滑稽的事情.所以我们要用Tarjan缩点.因为此题点权全为正,所以如果在图上走一个环当然可以全走 ...
- POJ 2553 The Bottom of a Graph Tarjan找环缩点(题解解释输入)
Description We will use the following (standard) definitions from graph theory. Let V be a nonempty ...
- Luogu3387 缩点 【tarjan】【DP】
Luogu3387 缩点 题目背景 缩点+DP 题目描述 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点, ...
- poj3694+hdu2460 求桥+缩点+LCA/tarjan
这个题使我更深理解了TARJAN算法,题意:无向图,每添加一条边后文桥的数量,三种解法:(按时间顺序),1,暴力,每每求桥,听说这样能过,我没过,用的hash判重,这次有俩个参数(n->10w, ...
- P1177 【模板】快速排序 题解
本质为sort,这里我们用优先队列(堆)解决. 什么是堆? 堆 堆是一个完全二叉树,而且是每层都有规律的二叉树 规律大概是: 小根堆:最上层数的大小最小,往下每层结点都比父亲结点大,比两个儿子结点小 ...
随机推荐
- Qt开发Activex笔记(一):环境搭建、基础开发流程和演示Demo
前言 使用C#开发动画,绘图性能跟不上,更换方案使用Qt开发Qt的控件制作成OCX以供C#调用,而activex则是ocx的更高级形式. QtCreator是没有Active控件项目的,所有需要 ...
- QUIC协议和HTTP3.0技术研究
QUIC:基于UDP的安全可靠的HTTP/2传输协议 摘要 QUIC(Quick UDP Internet Connection)是一个新的基于UDP的管线化技术和安全传输协议. QUIC提供: 和H ...
- 如何在手机上实现 H5 页面全屏显示
如何在手机上实现 H5 页面全屏显示 fullscreen 隐藏头部地址栏 隐藏底部导航栏 refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才 ...
- node.js module.exports & exports & module.export all in one
node.js module.exports & exports & module.export all in one cjs const log = console.log; log ...
- pub package all in one
pub package all in one best practice The pubspec file https://dart.dev/tools/pub/pubspec demo name: ...
- Google & Chrome console & text adventure game
Google & Chrome console & text adventure game Google's text adventure game https://www.googl ...
- html template tag
html template tag const tagName = `emoji-element`; const template = document.createElement('template ...
- how to install zoom meeting app in macOS
how to install zoom meeting app in macOS https://support.zoom.us/hc/zh-cn/articles/203020795-如何在Mac上 ...
- ES6 & Classes & Interface
ES6 & Classes & Interface what's the difference between javascript Classes & Interface ? ...
- Android 开发 权限管理
Android 开发 权限管理 https://sspai.com/post/42779 $ adb shell pm list permissions -d -g https://zhuanlan. ...