【模板】Tarjan 缩点 + 最长路
来源:互联网 发布:适合抓握的鼠标 知乎 编辑:程序博客网 时间:2024/06/04 18:00
#include<cstdio>#include<iostream>#include<cstring>#include<stack>#include<queue>#define maxn 101000using namespace std;typedef long long ll;stack <ll> S;queue <ll> Q;ll w[maxn],scc[maxn],scc_cnt;ll fst1[maxn],fst2[maxn],nxt1[maxn],nxt2[maxn];ll tot1,tot2,ans;ll dis[maxn],dis1[maxn];ll ru[maxn],dfn[maxn],low[maxn],cnt;bool vis[maxn];struct edge{ll from,to;}es1[maxn<<2],es2[maxn<<1];void build1(ll ff,ll tt){ es1[++tot1] = (edge){ff,tt}; nxt1[tot1] = fst1[ff]; fst1[ff] = tot1;}void build2(ll ff,ll tt){ es2[++tot2] = (edge){ff,tt}; nxt2[tot2] = fst2[ff]; fst2[ff] = tot2;}void tarjan(ll u){ S.push(u); dfn[u] = low[u] = ++cnt; for(ll i = fst1[u];i;i = nxt1[i]) { ll v = es1[i].to; if(!dfn[v]) { tarjan(v); low[u] = min(low[u],low[v]); } else if(!scc[v]) low[u] = min(low[u],dfn[v]); } if(dfn[u] == low[u]) { scc_cnt ++; while(true) { ll t = S.top(); S.pop(); scc[t] = scc_cnt; dis[scc_cnt] += w[t]; if(t == u) break; } }}void spfa(ll s){ memset(dis1,0,sizeof(dis1)); ans = max(ans,dis[s]); dis1[s]=dis[s]; Q.push(s); vis[s] = 1; while(!Q.empty()) { ll u = Q.front(); Q.pop(); vis[u] = 0; for(ll i = fst2[u];i;i = nxt2[i]) { ll v = es2[i].to; if(dis1[u] + dis[v] > dis1[v]) { dis1[v] = dis1[u]+dis[v]; ans = max(ans,dis1[v]); if(!vis[v]) { Q.push(v); vis[v] = 1; } } } }}int main(){ ll n,m; scanf("%lld%lld",&n,&m); for(ll i = 1;i <= n;i ++) scanf("%lld",&w[i]); for(ll i = 1;i <= m;i ++) { ll ff,tt; scanf("%lld%lld",&ff,&tt); build1(ff,tt); } for(ll i = 1;i <= n;i ++) if(!dfn[i]) tarjan(i); cout<<scc_cnt<<endl; for(int i=1;i<=scc_cnt;i++) cout<<dis[i]<<" scc "<<endl; for(ll i = 1;i <= tot1;i ++) { ll ff = es1[i].from; ll tt = es1[i].to; if(scc[ff] != scc[tt]) { build2(scc[ff],scc[tt]); cout<<scc[ff]<<"->"<<scc[tt]<<endl; } } for(int i=1;i<=scc_cnt;i++) build2(0,i); spfa(0); printf("%lld",ans); return 0;}最短路同理 处理Spfa即可
阅读全文
0 0
- 【模板】Tarjan 缩点 + 最长路
- Tarjan缩点模板
- poj 3592 Instantaneous Transference tarjan缩点 最长路 ++tarjan模版 && spfa最长路模版
- poj 3592 Instantaneous Transference(tarjan + 缩点 + 最长路)
- bzoj 1179(tarjan缩点+最长路)
- NKOJ 3213 牧草鉴赏家(Tarjan缩点+最长路)
- [SDOI2010]所驼门王的宝藏 --tarjan缩点+最长路
- 【BZOJ1179】atm,tarjan缩点+最长路径
- 【hh大神的】Tarjan + 缩点 模板
- tarjan缩点模板 poj 2186
- tarjan算法缩点构图(模板)
- Tarjan 缩点模板(洛谷P3387)
- Tarjan缩点模板(洛谷P3387)
- tarjan缩点/求桥模板
- poj 2186 DAG+缩点+tarjan模板 (模板待消化
- 模板整理: 图论---tarjan缩点/桥/割点
- POJ 3160 Father Christmas flymouse tarjan缩点+spfa求最长路
- 1924: [Sdoi2010]所驼门王的宝藏 tarjan缩点+dp最长路
- 017 含参方程组之习题应用
- 【bzoj2216】[Poi2011]Lightning Conductor
- 列表的使用---深浅copy的区别
- Linux 内核引导选项简介
- vue里面各文件说明
- 【模板】Tarjan 缩点 + 最长路
- 吴恩达deeplearning.ai课程系列笔记10
- Xcode9学习笔记45
- linux中ftp的配置文件祥解
- 我的面试总结
- Alex and broken contest Codeforces Round #442
- Mac环境下python-opencv的安装
- LuoguP1074[NOIP2009] 靶形数独 解题报告【搜索】
- lca模板