= =这题写搓了。。。
= =,,第一次的DP方程思路是错的。。
后来又忘记初始化。。= =
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
const int N = 30011;
const int M = 150111;
int n,m;
int head[N],tot,val[N],cnt,pre[N],low[N];
int s[N],stop,id[N];
int sum[N],head2[N],out_degree[N];
struct node{
int v,ne;
}e[M<<1];
void addedge(int u,int v,int h[]){
e[cnt].ne = h[u], e[cnt].v = v ,h[u]=cnt++;
}
void dfs(int u,int n){
int t,minc=low[u]=pre[u]=cnt++;
s[stop++]=u;
for(int q=head[u]; q!=-1 ; q=e[q].ne){
int v=e[q].v;
if(-1==pre[v]) dfs(v,n);
if(low[v]<minc) minc=low[v];
}
if(minc<low[u]){
low[u]=minc; return;
}
do{
id[t=s[--stop]]=tot,low[t]=n;
sum[tot]+=val[t];
}while(t!=u);
++tot;
}
int dp[N];
int bfs(){
memset(dp,0,sizeof(dp));
int i,x;
int ans = 0;
queue<int> q;
for(int i=0;i<tot;i++)
if(out_degree[i]==0) q.push(i),dp[i]=sum[i];
while(!q.empty()){
x = q.front(); q.pop();
ans = ans > dp[x] ? ans : dp[x];
for(int k = head2[x]; k!=-1 ; k=e[k].ne){
int v = e[k].v;
out_degree[v]--;
dp[v] = max(dp[v], dp[x]+sum[v]);
if(out_degree[v]==0) q.push(v);
}
}
return ans;
}
int main(){
while(scanf("%d %d",&n,&m)!=EOF){
memset(head,-1,sizeof(head));
cnt=0;
for(int i = 0;i < n;i++){
scanf("%d",val+i);
if(val[i]<0) val[i]=0;
}
for(int i = 0,u,v; i<m;i++){
scanf("%d %d",&u,&v);
addedge(u,v,head);
}
int ed_num=cnt;
stop=tot=cnt=0;
memset(sum,0,sizeof(sum));
memset(pre,-1,sizeof(pre));
for(int i=0;i<n;i++)
if(pre[i] == -1)
dfs(i,n+1);
memset(head2,-1,sizeof(head));
memset(out_degree,0,sizeof(out_degree));
cnt=ed_num;
for(int u=0;u<n;u++)
for(int i=head[u];i!=-1;i=e[i].ne){
int v=e[i].v;
if(id[u]==id[v])continue;
addedge(id[u],id[v],head2);
out_degree[id[v]]++;
}
int ret = bfs();
printf("%d\n",ret);
}
return 0;
}