解题思路

缩点后按拓扑排序跑一个dp。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue> using namespace std;
const int MAXN = ;
const int MAXM = ; inline int rd(){
int x=,f=;char ch=getchar();
while(!isdigit(ch)) {f=ch=='-'?:;ch=getchar();}
while(isdigit(ch)) {x=(x<<)+(x<<)+ch-'';ch=getchar();}
return f?x:-x;
} int n,m,head[MAXN],cnt,to[MAXM],nxt[MAXM],w[MAXN],wt[MAXN],ans;
int head_[MAXN],cnt_,to_[MAXM],nxt_[MAXM],du[MAXN],f[MAXN];
int dfn[MAXN],low[MAXN],num,stk[MAXN],top,col_num,col[MAXN];
bool vis[MAXN];
queue<int> Q; inline void add(int bg,int ed){
to[++cnt]=ed,nxt[cnt]=head[bg],head[bg]=cnt;
}
void tarjan(int x){
dfn[x]=low[x]=++num;
stk[++top]=x;vis[x]=;
for(register int i=head[x];i;i=nxt[i]){
int u=to[i];
if(!dfn[u]) tarjan(u),low[x]=min(low[x],low[u]);
else if(vis[u]) low[x]=min(dfn[u],low[x]);
}
if(low[x]!=dfn[x]) return;col_num++;
while(stk[top]!=x) {
wt[col_num]+=w[stk[top]];
vis[stk[top]]=;
col[stk[top--]]=col_num;
}top--;
vis[x]=;col[x]=col_num;wt[col_num]+=w[x];
} inline void add_(int bg,int ed){
to_[++cnt_]=ed,nxt_[cnt_]=head_[bg],head_[bg]=cnt_;
} int main(){
n=rd(),m=rd();int x,y,u;
for(int i=;i<=n;i++) w[i]=rd();
for(int i=;i<=m;i++){
x=rd(),y=rd();
add(x,y);
}
for(int i=;i<=n;i++) if(!dfn[i]) tarjan(i);
for(int i=;i<=n;i++)
for(int j=head[i];j;j=nxt[j]){
u=to[j];
if(col[u]!=col[i]) add_(col[i],col[u]),du[col[u]]++;
}
for(int i=;i<=col_num;i++) if(!du[i]) Q.push(i),f[i]=wt[i];
while(!Q.empty()){
int x=Q.front();Q.pop();
for(int i=head_[x];i;i=nxt_[i]){
int u=to_[i];
f[u]=max(f[u],f[x]+wt[u]);
du[u]--;if(!du[u]) Q.push(u);
}
}
for(int i=;i<=col_num;i++) ans=max(ans,f[i]);
printf("%d",ans);
return ;
}

LUOGU P3387 【模板】缩点 (缩点+DAG dp)的更多相关文章

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

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

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

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

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

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

  4. Luogu P2742 模板-二维凸包

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

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

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

  6. Luogu P2272 [ZJOI2007]最大半连通子图(Tarjan+dp)

    P2272 [ZJOI2007]最大半连通子图 题意 题目描述 一个有向图\(G=(V,E)\)称为半连通的\((Semi-Connected)\),如果满足:\(\forall u,v\in V\) ...

  7. (板子)缩点 + DAG上的DP(深搜)luogu P3387

    板子传送门 根据题目意思,我们只需要找出一条点权最大的路径就行了,不限制点的个数.那么考虑对于一个环上的点被选择了,一整条环是不是应该都被选择,这一定很优,能选干嘛不选.很关键的是题目还允许我们重复经 ...

  8. luogu P3387 【模板】缩点

    题目 好久没法博客了 这次就水个板子题目吧 tarjan缩点之后重新建图 而且边权应该都是正的(要不我怎么能这么轻松水过去) 在新图上记忆化一下就好了 f[i] 表示 开头选i这个点 的 路径最大值 ...

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

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

随机推荐

  1. 第37讲 谈谈Spring Bean的生命周期和作用域

    在企业应用软件开发中,Java 是毫无争议的主流语言,开放的 Java EE 规范和强大的开源框架功不可没,其中 Spring 毫无疑问已经成为企业软件开发的事实标准之一.今天这一讲,我将补充 Spr ...

  2. 新工具Scapy

    新工具 Scapy 1.环境: 命令: pip install scapy 启动终端: 看这花里胡哨的界面那就成功啦! 注意上图中的INFO信息,如果没有安装可选包,部分功能不可用,在需要的时候单独安 ...

  3. LeetCode 31. Next Permutation【Medium】

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  4. Error resolving template,template might not exist or might not be accessible by any of the configured Template Resolvers

    template might not exist or might not be accessible by any of the configured Template Resolvers at o ...

  5. Docker学习のDocker和虚拟机

    最初听到Docker,是作为虚拟机来宣传的,但是它本质不是虚拟机 一.虚拟机 虚拟机(Virtual Machine)指通过软件模拟的具有完整硬件系统功能的.运行在一个完全隔离环境中的完整计算机系统. ...

  6. java笔试之查找组成一个偶数最接近的两个素数

    任意一个偶数(大于2)都可以由2个素数组成,组成偶数的2个素数有很多种情况,本题目要求输出组成指定偶数的两个素数差值最小的素数对. package test; import java.util.Sca ...

  7. .net中的泛型全面解析

    从2.0起我们一直就在谈论泛型,那么什么是泛型,泛型有什么好处,与泛型相关的概念又该怎么使用,比如泛型方法,泛型委托.这一篇我会全面的介绍泛型. 那么首先我们必须搞清楚什么是泛型,泛型其实也是一种类型 ...

  8. arm-linux-objdump 的使用

    1. 查看静态库或.o 文件的组成文件 [arm@localhost gcc]$ arm­linux­objdump ­a libhello.a 2. 查看静态库或.o 文件的络组成部分的头部分 [a ...

  9. Nginx配置两份日志记录

    nginx配置 版本-1.4.4 --- access_log /alidata/log/nginx/access/wordpress1.log ; access_log /alidata/log/n ...

  10. springboot+mybatis 非web项目构建

    https://blog.csdn.net/wlittlefive/article/details/86157134 https://blog.csdn.net/ththcc/article/deta ...