60分求助
查看原帖
60分求助
149933
Zero_Legend楼主2021/2/5 08:20
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,m;
int tim;
int top;
int idx,head[N];
int low[N],dfn[N];
int sd[N];
int st[N];
int vis[N];
int in[N];
int p[N];
int size[N];
int dp[N];
int ans=0,res=0;
int fg;
struct node {
	int from;
	int to;
	int nex;
} edge[N],eg[N];

void add1(int x,int y) {
	eg[++idx].from=x;
	eg[idx].to=y;
	eg[idx].nex=head[x];
	head[x]=idx;
}

void add2(int x,int y) {
	in[y]++;
	edge[++idx].from=x;
	edge[idx].to=y;
	edge[idx].nex=head[x];
	head[x]=idx;
}

void tarjan(int x) {
	dfn[x]=low[x]=++tim;
	st[++top]=x;
	sd[x]=x;
	vis[x]=1;
	for(int i=head[x];i;i=eg[i].nex){
		int y=eg[i].to;
		if(!dfn[y]) {
			tarjan(y);
			low[x]=min(low[x],low[y]);
		} else if(vis[y]) low[x]=min(low[x],dfn[y]);
	}
	if(low[x]==dfn[x]) {
		int y;
		while(y=st[top--]) {
			sd[y]=x;
			vis[y]=0;
			if(x==y)break;
			p[x]+=p[y];
		}
	}
}

void topu(int x,int fa){
	for(int i=head[x];i;i=edge[i].nex){
		int y=edge[i].to;
		if(y==fa) return ;
		in[y]--;
		dp[y]=dp[x]+p[sd[y]];
		if(!in[y]) topu(y,x);
	}
}

int main() {
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>p[i];
	}
    for(int i=1;i<=m;i++){
    	int a,b;
    	cin>>a>>b;
    	add1(a,b);
	}
	for(int i=1;i<=n;i++){
		if(!dfn[i]) tarjan(i);
	}
	idx=0;
	memset(head,0,sizeof(head));
	
	for(int i=1;i<=m;i++){
		int x=sd[eg[i].from];
		int y=sd[eg[i].to];
		if(x!=y) add2(x,y);
	}
	for(int i=1;i<=n;i++){
		dp[i]=p[sd[i]];
	}
	for(int i=1;i<=n;i++){
		if(!in[i]&&sd[i]==i) topu(sd[i],0);
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		ans=max(ans,dp[sd[i]]);
	}
	cout<<ans;
	return 0;
}
2021/2/5 08:20
加载中...