题目

好久没法博客了

这次就水个板子题目吧

tarjan缩点之后重新建图

而且边权应该都是正的(要不我怎么能这么轻松水过去)

在新图上记忆化一下就好了

f[i] 表示 开头选i这个点 的 路径最大值

#include <bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=1e5+7;
int n,m,w[maxn],f[maxn];
int stak[maxn],top,vis[maxn],dfn[maxn],low[maxn],cnt;//一套trajan
int color[maxn],color_tot,color_w[maxn];//一套染色数组
struct edge {
int u,v,nxt;
}old_e[maxn],new_e[maxn];
int old_head[maxn],old_tot;
int new_head[maxn],new_tot;
void add1(int u,int v) {
old_e[++old_tot].u=u;
old_e[old_tot].v=v;
old_e[old_tot].nxt=old_head[u];
old_head[u]=old_tot;
}
void add2(int u,int v) {
new_e[++new_tot].v=v;
new_e[new_tot].nxt=new_head[u];
new_head[u]=new_tot;
}
void tarjan(int u) {
dfn[u]=low[u]=++cnt;
vis[u]=1;
stak[++top]=u;
for(int i=old_head[u];i;i=old_e[i].nxt) {
int v=old_e[i].v;
if(!dfn[v]) {
tarjan(v);
dfn[u]=min(dfn[u],dfn[v]);
} else
if(vis[v])
dfn[u]=min(dfn[u],low[v]);
}
if(dfn[u]==low[u]) {
++color_tot;
while(stak[top]!=u) {
vis[stak[top]]=0;
color_w[color_tot]+=w[stak[top]];
color[stak[top--]]=color_tot;
}
top--;
color[u]=color_tot;
color_w[color_tot]+=w[u];
vis[u]=0;
}
}
//f[i] 表示 开头选i这个点的路径最大值
int dfs(int u)
{
if(f[u]) return f[u];
int tmp=0;
for(int i=new_head[u];i;i=new_e[i].nxt) {
int v=new_e[i].v;
tmp=max(tmp,dfs(v));
}
if(!tmp) return f[u]=color_w[u];
return f[u]=color_w[u]+tmp;
}
int main()
{
//freopen("testdata.in","r",stdin);
//读入
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)
scanf("%d",&w[i]);
for(int i=1;i<=m;++i) {
int u,v;
scanf("%d%d",&u,&v);
add1(u,v);
}
//缩点
for(int i=1;i<=n;++i)
if(!dfn[i])
tarjan(i);
//建新图
for(int i=1;i<=old_tot;++i)
if(color[old_e[i].u]!=color[old_e[i].v])
{
add2(color[old_e[i].u],color[old_e[i].v]);
//cout<<color[old_e[i].u]<<" -> "<<color[old_e[i].v]<<"\n";
}
//记忆化搜索
int ans=0;
for(int i=1;i<=color_tot;++i)
ans=max(ans,dfs(i));
//输出ans
printf("%d\n",ans);
return 0;
}

luogu P3387 【模板】缩点的更多相关文章

  1. 【Luogu P3387】缩点模板(强连通分量Tarjan&拓扑排序)

    Luogu P3387 强连通分量的定义如下: 有向图强连通分量:在有向图G中,如果两个顶点vi,vj间(vi>vj)有一条从vi到vj的有向路径,同时还有一条从vj到vi的有向路径,则称两个顶 ...

  2. [luogu P3384] [模板]树链剖分

    [luogu P3384] [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点 ...

  3. Luogu P2742 模板-二维凸包

    Luogu P2742 模板-二维凸包 之前写的实在是太蠢了.于是重新写了一个. 用 \(Graham\) 算法求凸包. 注意两个向量 \(a\times b>0\) 的意义是 \(b\) 在 ...

  4. luogu P3919 [模板]可持久化数组(可持久化线段树/平衡树)(主席树)

    luogu P3919 [模板]可持久化数组(可持久化线段树/平衡树) 题目 #include<iostream> #include<cstdlib> #include< ...

  5. 解题报告+板子:luogu P3387 【模板】缩点

    题目链接:P3387 [模板]缩点 缩点板子,所谓\(dp\)就是拓扑排序(毕竟可以重走边),像\(SPFA\)一样松弛就好,就是重边极其烦人,还加了排序(绝对自己想的,然鹅拓扑的思路不是). 下面上 ...

  6. LUOGU P3387 【模板】缩点 (缩点+DAG dp)

    解题思路 缩点后按拓扑排序跑一个dp. #include<iostream> #include<cstdio> #include<cstring> #include ...

  7. luogu P3387 【模板】缩点_拓扑排序

    还是很好些的. Code: #include <stack> #include <cstdio> #include <algorithm> #include < ...

  8. Tarjan+topsort(DP)【P3387】 [模板]缩点

    Description 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只计算一次 ...

  9. [模板](luogu P3387)縮點

    前言:對於這週的咕咕咕表示好像沒什麼好表示的,完全沒有靈感a......寫東西真的好難啊......於是又玩了半天鬼泣4???還挺好玩的 來源:題解 题目背景 缩点+DP 题目描述 给定一个n个点m条 ...

随机推荐

  1. Silver Cow Party---poj3268(最短路,迪杰斯特拉)

    Silver Cow Party Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u De ...

  2. IDEA的快捷键和相关设置

      快捷键 Shift + Shift: 查找一切 Alt + /: 代码提示(需要修改) Ctrl + Alt + F12: 打开文件所在磁盘位置 Alt + F12: 打开终端 Alt + Ins ...

  3. npm的用户名添加不上的原因

    npm添加不上的错误e401 1.用cnpm提交,会提交的tao.org这个域名了,用npm提交试试 2.如果npm提交不上,那就查看配置文件配置中 registry=http://registry. ...

  4. SQL Server 学习博客分享列表(应用式学习 + 深入理解)

    SQL Server 学习博客分享列表(应用式学习 + 深入理解) 转自:https://blog.csdn.net/tianjing0805/article/details/75047574 SQL ...

  5. Shell初学(四)运算符

    一.算术运算符 下表列出了常用的算术运算符,假定变量 a 为 10,变量 b 为 20: 运算符 说明 举例 + 加法 `expr $a + $b` 结果为 30. - 减法 `expr $a - $ ...

  6. PHPExcel使用-使用PHPExcel导出文件

    导出步骤: 1. 新建一个excel表格 ------------> 实例化PHPExcel类 2. 创建sheet(内置表)-------------> ( 1>. createS ...

  7. GOLANG错误处理最佳方案errors wrap, Defer, Panic, and Recover

    Simple error handling primitives:        https://github.com/pkg/errors Defer, Panic, and Recover:    ...

  8. [py][mx]django自带后台系统使用

    django的manytomany字段和后台搜索过滤功能 后台开发一般要求 后台要求能快速搭建, 主要精力放在前端用户系统开发上. 权限管理 少量样式 快速开发 django自带的后台手动注册模型 创 ...

  9. BCB ERROR:[Linker Error] 'XXX.LIB' contains invalid OMF record, type 0x21 (possibly COFF)

    今天C++builder 导入 gts .lib  (gts.dll)库文件 编译报错: [Linker Error] 'D:\...\V4.05.007.1000-20161028\GTS.LIB' ...

  10. PIMPL(一)

    1 参考 <effective C++> 条款31:将文件间的编译关系降至最低 PIMPL Idiom: http://c2.com/cgi/wiki?PimplIdiom 2 什么是PI ...