WA80pts求助,#4 #7
查看原帖
WA80pts求助,#4 #7
1112954
lsc72楼主2024/8/31 16:56
#include<bits/stdc++.h>
using namespace std;
stack<int> s;
int n,m,low[100010],dfn[100010],vis[100010],dp[100010],in[100010],p,np[100010],w[100010],nw[1000010],q;
int u[100010],v[100010],ans;
vector<int> es[100010];
vector<int> ne[100010];
queue<int> t;
void tanjan(int x){
	vis[x]=1;
	dfn[x]=++p;
	low[x]=dfn[x];
	s.push(x);
	for (int e:es[x]){
		if (!vis[e]){
			tanjan(e);
		}
		low[x]=min(low[e],low[x]);
	}
	if (low[x]==dfn[x]){
		q++;
		while (!s.empty()&&dfn[s.top()]>=dfn[x]){
			nw[q]+=w[s.top()];
			dp[q]+=w[s.top()];
			np[s.top()]=q;
			s.pop();
		}
	}
}
int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin>>n>>m;
	for (int i=1;i<=n;i++) cin>>w[i],low[i]=1e9;
	for (int i=1;i<=m;i++){
		cin>>u[i]>>v[i];
		es[u[i]].push_back(v[i]);
	}
	for (int i=1;i<=n;i++) if (!vis[i]) tanjan(i);
	while (!s.empty()){
		q++;
		nw[q]+=w[s.top()];
		dp[q]+=w[s.top()];
		np[s.top()]=q;
		s.pop();
	}
	for (int i=1;i<=m;i++){
		if (np[u[i]]!=np[v[i]]){
			ne[np[u[i]]].push_back(np[v[i]]);
		}
	}
	for (int i=1;i<=q;i++) t.push(i);
	while (!t.empty()){
		int f=t.front();
		t.pop();
		for (int e:ne[f]){
			if (dp[f]+nw[e]>dp[e]){
				t.push(e);
				dp[e]=dp[f]+nw[e];
			}
		}
	}
	for (int i=1;i<=q;i++) ans=max(ans,dp[i]);
	cout<<ans;
}
2024/8/31 16:56
加载中...