LUOGU P3387 【模板】缩点 (缩点+DAG dp)
解题思路
缩点后按拓扑排序跑一个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)的更多相关文章
- 【Luogu P3387】缩点模板(强连通分量Tarjan&拓扑排序)
Luogu P3387 强连通分量的定义如下: 有向图强连通分量:在有向图G中,如果两个顶点vi,vj间(vi>vj)有一条从vi到vj的有向路径,同时还有一条从vj到vi的有向路径,则称两个顶 ...
- 解题报告+板子:luogu P3387 【模板】缩点
题目链接:P3387 [模板]缩点 缩点板子,所谓\(dp\)就是拓扑排序(毕竟可以重走边),像\(SPFA\)一样松弛就好,就是重边极其烦人,还加了排序(绝对自己想的,然鹅拓扑的思路不是). 下面上 ...
- [luogu P3384] [模板]树链剖分
[luogu P3384] [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点 ...
- Luogu P2742 模板-二维凸包
Luogu P2742 模板-二维凸包 之前写的实在是太蠢了.于是重新写了一个. 用 \(Graham\) 算法求凸包. 注意两个向量 \(a\times b>0\) 的意义是 \(b\) 在 ...
- luogu P3919 [模板]可持久化数组(可持久化线段树/平衡树)(主席树)
luogu P3919 [模板]可持久化数组(可持久化线段树/平衡树) 题目 #include<iostream> #include<cstdlib> #include< ...
- Luogu P2272 [ZJOI2007]最大半连通子图(Tarjan+dp)
P2272 [ZJOI2007]最大半连通子图 题意 题目描述 一个有向图\(G=(V,E)\)称为半连通的\((Semi-Connected)\),如果满足:\(\forall u,v\in V\) ...
- (板子)缩点 + DAG上的DP(深搜)luogu P3387
板子传送门 根据题目意思,我们只需要找出一条点权最大的路径就行了,不限制点的个数.那么考虑对于一个环上的点被选择了,一整条环是不是应该都被选择,这一定很优,能选干嘛不选.很关键的是题目还允许我们重复经 ...
- luogu P3387 【模板】缩点
题目 好久没法博客了 这次就水个板子题目吧 tarjan缩点之后重新建图 而且边权应该都是正的(要不我怎么能这么轻松水过去) 在新图上记忆化一下就好了 f[i] 表示 开头选i这个点 的 路径最大值 ...
- luogu P3387 【模板】缩点_拓扑排序
还是很好些的. Code: #include <stack> #include <cstdio> #include <algorithm> #include < ...
随机推荐
- hash值的计算与转换 分类: ACM TYPE 2015-05-07 17:49 36人阅读 评论(0) 收藏
#include <bits/stdc++.h> using namespace std; const int MAXN = 100; const int X = 3; long long ...
- Day 7 :一句话Python(匿名函数-lambda,三元运算,列表表达式,生成器表达式)
注意: 1.所有的列表表达式都可以转换成生成器表达式 2.经量让标傲世简化你得操作,增加代码可读性 3.如果代码过于复杂,应该转换成普通代码 4.再代码中尽可能多使用生成器表达式. 三元运算符:简化代 ...
- 利用IK分词器,自定义分词规则
IK分词源码下载地址:https://code.google.com/p/ik-analyzer/downloads/list lucene源码下载地址:http://www.eu.apache.or ...
- spark2.+ sql 性能调优
1.在内存中缓存数据 性能调优主要是将数据放入内存中操作,spark缓存注册表的方法 版本 缓存 释放缓存 spark2.+ spark.catalog.cacheTable("tableN ...
- kafka 扩展partition和replication-factor
问题: 1. kafka的topic 是程序自己建立,默认只建立8个partitions,1个replication-factor 目的: 扩展partitions 到9个, replicatoion ...
- 修改jupyter保存文件目录
参考: https://blog.csdn.net/lyxuefeng/article/details/79469189 步骤 打开 cmd 输入命令 jupyter notebook --gener ...
- XSS攻击原理
本文转载的地址:http://www.2cto.com/Article/201209/156182.html Xss(cross-site scripting)攻击指的是攻击者往Web页面里插入恶意h ...
- Redhat镜像-RHEL-官方镜像下载大全
原网站内容链接:https://pan.baidu.com/s/12XYXh#list/path=%2F 已经存在自己的云盘上了
- Redis中取出值,转成对象
import com.fasterxml.jackson.databind.ObjectMapper; //转成companyEntity CompanyEntity company = mapper ...
- Block的Retain Cycle的解决方法
一个使用Block语法的实例变量,在引用另一个实例变量的时候,经常会引起retain cycle.这个问题在使ASIHTTPRequest的block语法的时候会时不时的碰到.这个问题困扰了我这个小白 ...