首页 技术 正文
技术 2022年11月6日
0 收藏 619 点赞 561 浏览 1551 个字

解题思路

缩点后按拓扑排序跑一个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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289