40pts求调
查看原帖
40pts求调
976798
lsy621楼主2024/7/18 17:09

40pts

#include<bits/stdc++.h>
#define N 100010
#define M 200010
using namespace std;
int n,m,ans,cntc,temp;
int a[N],node[N];
int tot,head[N],to[M],nxt[M],val[M];
int depth,top,z[N],v[N],in[N],deep[N],low[N];
int group[N],arr[N],f[N];
int d[N];
vector <int> scc[N];
struct rec{
	int x,y,z;
}str[M];
void add(int x,int y){to[++tot]=y;nxt[tot]=head[x];head[x]=tot;}
void add(int x,int y,int z){to[++tot]=y;val[tot]=z;nxt[tot]=head[x];head[x]=tot;}
void tarjan(int x){
	z[++top]=x,v[x]=1,in[x]=1;
	deep[x]=low[x]=++depth;
	for(int i=head[x];i;i=nxt[i]){
		int y=to[i];
		if(!v[y]){
			tarjan(y);
			low[x]=min(low[x],low[y]);
			if(low[y]>deep[x]){
				str[++temp].x=x;
				str[temp].y=y;
			}
		}
		else if(in[y]){
			low[x]=min(low[x],deep[y]);
		}
	}
	if(low[x]==deep[x]){
		cntc++;
		do{
			scc[cntc].push_back(z[top--]);
			in[scc[cntc].back()]=0;
		}while(scc[cntc].back()!=x);
	}
}
void shrink(){
	for(int i=1;i<=cntc;i++){
		vector<int>::iterator x;
		for(x=scc[i].begin();x<scc[i].end();x++){
			group[*x]=i;
			node[i]+=a[*x];
		}
	}
	memset(head,0,sizeof(head));
	memset(to,0,sizeof(to));
	memset(nxt,0,sizeof(nxt));
	memset(v,0,sizeof(v));
	tot=0;
	for(int i=1;i<=temp;i++){
		add(group[str[i].x],group[str[i].y],str[i].z);
	}
}
void dfs(int x){
	if(f[x])return;
	int maxx=0;
	f[x]=node[x];
	for(int i=head[x];i;i=nxt[i]){
		int y=to[i];
		if(!f[y])dfs(y);
		maxx=max(maxx,f[y]);
	}
	f[x]+=maxx;
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1,x,y;i<=m;i++){
		scanf("%d%d",&x,&y);
		add(x,y);
	}
	for(int i=1;i<=n;i++){
		if(!deep[i])tarjan(i);	
	}
	shrink();
	for(int i=1;i<=cntc;i++)if(!f[i]){
		dfs(i);
	}
	for(int i=1;i<=cntc;i++)ans=max(ans,f[i]);
	printf("%d",ans);
	return 0;
}
2024/7/18 17:09
加载中...