1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <algorithm> using namespace std;
const int maxn = 6000+5;
int dp[maxn][2]; int fa[maxn]; int in[maxn]; bool vis[maxn]; int n;
struct { int v; int next; }e[maxn<<1];
int cnt,head[maxn];
void addedge(int u,int v) { e[cnt].v=v; e[cnt].next=head[u]; head[u]=cnt++; }
void dfs(int u) { vis[u]=true; for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].v; dfs(v); dp[u][0] += max(dp[v][0], dp[v][1]); dp[u][1] += dp[v][0]; } } int main() { freopen("in","r",stdin); while(scanf("%d",&n)!=EOF) { memset(vis,0,sizeof(vis)); memset(dp,0,sizeof(dp)); memset(in,0,sizeof(in)); cnt=0; memset(head,-1,sizeof(head));
for (int i=1;i<=n;i++) scanf("%d",&dp[i][1]); int u,v; while(scanf("%d%d",&v,&u)!=EOF && v && u) { addedge(u,v); in[v]++; } for (int i=1;i<=n;i++) { if (in[i]==0) { dfs(i); printf("%dn",max(dp[i][0],dp[i][1])); break; } } } return 0; }
|