版本比较
缩点题解
正文
增加 11 行删除 11 行
123434567891011111213141517 行未改动3334353637373839404119 行未改动6162636465666768656667686970707172737414 行未改动8990919293939495969742 行未改动1401411421431441441451461471487 行未改动# 缩点## 题目描述给定一个 $n $个点 给定一个$n $个点$m$ 条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大。你只需要求出这个权值和。
**允许多次经过一条边或者一个点**,但是,重复经过的点,权值只计算一次。## 题目分析要求所有路径经过的点权的最大值,一般来说对于有向无环图$(DAG)$,我们可以使用拓扑排序+DP来求解,状态转移方程如下:
$dp[u]+max(dp[v])$ $dp[u]+max(dp[v])$
但是该题目没有明确说一定是$DAG$图,所以我们需要用强连通分量来将有向图转化为$DAG$图,举例如下:
17 行未改动 do{ x=st.top(); st.pop(); sccno[x]=scc_cnt; sa[scc_cnt]+=a[x]; sa[scc_cnt]+=a[x]; }while(x!=u); } return;}19 行未改动```cpp#include<bits/stdc++.h>using namespace std;const int N=1e4+5;int dp[N]; int in[N];//in[x]表示x的入度 int dfn[N];//dfn[x]表示x的时间戳 int low[N];//low[x]表示x能回到的最小时间戳 int dp[N];int in[N];//in[x]表示x的入度int dfn[N];//dfn[x]表示x的时间戳int low[N];//low[x]表示x能回到的最小时间戳int sccno[N];//sccno[x]表示x所在的强连通分量编号int sa[N];//sa[x]表示x的强连通分量的点权和 int sa[N];//sa[x]表示x的强连通分量的点权和int a[N];int scc_cnt,dfs_clock,u,v,n,m;stack<int> st;vector<int> G1[N],G2[N];14 行未改动 do{ x=st.top(); st.pop(); sccno[x]=scc_cnt; sa[scc_cnt]+=a[x]; sa[scc_cnt]+=a[x]; }while(x!=u); } return;}42 行未改动 } for(int i=1;i<=n;i++){ if(!dfn[i]){ tarjan(i); } } } remake(); kahn(); int ans=0;7 行未改动