#include<bits/stdc++.h>
using namespace std;
int read() { //快读
int s = 0, ne = 1; char c = getchar();
for (; c < '0' || c>'9'; c = getchar()) if (c == '-') ne = -1;
for (; c >= '0' && c <= '9'; c = getchar()) s = (s << 1) + (s << 3) + c - '0';
return s * ne;
}
int vis[100005];
int low[100005];
int scc[100005];
int cnt,scc_cnt;
int weight[100005];
vector<vector<int>> graph;
stack<int> s;
void dfs(int x)
{
int mini = ++cnt;
vis[x] = mini;
s.push(x);
for (int i = 0; i < graph[x].size(); i++)
{
int w = graph[x][i];
if (vis[w] == 0) //树枝边
{
dfs(w);
mini = min(low[w],mini);
}
else if (scc[w] != 0) //若是横叉边 则不需要操作
{
mini = min(mini,vis[w]); //前向边与后向边
}
}
low[x] = mini;
if (low[x] == vis[x])
{
scc_cnt++;
while (1)
{
int cur = s.top(); s.pop();
scc[cur] = scc_cnt;
if (cur == x)
break;
}
}
}
int ans=0;
vector<int> memo;
vector<vector<int>> dag;
vector<int> dag_weight;
void dag_dp(int x)
{
if (memo[x] != -1)
return;
int tmp = 0;
for (auto i :dag[x])
{
if (memo[i] == -1)
dag_dp(i);
tmp = max(tmp, memo[i]);
}
memo[x] = tmp + dag_weight[x];
}
int main()
{
ios::sync_with_stdio(false);
int n = read();
int m = read();
memset(weight, 0, sizeof weight);
for (int i = 1; i <= n; i++)
weight[i] = read();
graph.resize(n + 1);
int u, v;
for (int i = 1; i <= m; i++)
{
u = read(); v = read();
graph[u].push_back(v);
}
cnt = 0;
scc_cnt = 0;
memset(low,0,sizeof low);
memset(vis, 0, sizeof vis);
memset(scc, 0, sizeof scc);
for(int i=1;i<=n;i++)
{
if(vis[i]==0)
dfs(i);
}
// dag 建图
dag.resize(scc_cnt+1);
dag_weight.resize(scc_cnt+1);
for (int i = 1; i <= scc_cnt; i++)
dag_weight[i] = 0;
for (int i=1;i<=n;i++)
{
dag_weight[scc[i]] += weight[i];
for (int j : graph[i])
{
if(scc[j]!=scc[i])
dag[scc[i]].emplace_back(scc[j]);
}
}
memo.resize(scc_cnt+1);
for (int i = 1; i <= scc_cnt; i++)
memo[i] = -1;
for (int i = 1; i <= scc_cnt; i++)
{
dag_dp(i);
ans = max(ans,memo[i]);
}
cout << ans;
}
全部超空间。。。 下载数据下来也没问题啊。。