#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10;
int n, m;
vector <int> e[N], ne[N];
int dfn[N], low[N], tot;
int stk[N], instk[N], top;
int scc[N], cnt;
int w[N], nw[N], dp[N];
void tarjan(int x)
{
dfn[x] = low[x] = ++tot;
stk[++top] = x, instk[x] = 1;
for(int y : e[x])
{
if(!dfn[y])
{
tarjan(y);
low[x] = min(low[x], low[y]);
}
else if(instk[x])
low[x] = min(low[x], dfn[y]);
}
if(dfn[x] == low[x])
{
int y; ++ cnt;
do
{
y = stk[top--];
instk[y] = 0;
scc[y] = cnt;
} while (y != x);
}
}
int main()
{
int a, b, ans = 0;
cin >> n >> m;
for(int i = 1; i <= n; i++) cin >> w[i];
for(int i = 1; i <= m; i++)
{
cin >> a >> b;
e[a].push_back(b);
}
for(int i = 1; i <= n; i++)
if(!dfn[i]) tarjan(i);
for(int x = 1; x <= n; x++)
{
nw[scc[x]] += w[x];
for(int y : e[x])
{
a = scc[x], b = scc[y];
if(a != b) ne[a].push_back(b);
}
}
for(int x = cnt; x; x --)
{
if(dp[x] == 0)
dp[x] = nw[x];
for(int y : ne[x])
dp[y] = max(dp[y], dp[x]+nw[y]);
}
for(int i = 1; i <= cnt; i++)
ans = max(ans, dp[i]);
cout << ans << endl;
return 0;
}
错误的测试点 : WA #2
in : 10 20 970 369 910 889 470 106 658 659 916 964 3 2 3 6 3 4 9 5 8 3 5 8 9 1 9 7 9 8 7 5 3 7 7 8 1 7 10 2 1 10 4 8 2 6 3 1 3 5 8 5
out(ans): 6911 mine : 5995