Time Limit: 1000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
Description
Input
Output
Sample Input
7 1 1 1 1 1 1 1 1 3 2 3 6 4 7 4 4 5 3 5 0 0
Sample Output
5
Source
#include <iostream> #include <cstdio> #include <cstring> using namespace std; #define maxn 6000+5 int n,rat[maxn],fa[maxn],dp[maxn][2],vis[maxn]; void dfs(int x ) { vis[x] = 1; for(int i = 1;i <= n;i ++) { if(!vis[i] && fa[i] == x) { dfs(i); dp[x][0] += max(dp[i][0], dp[i][1]); dp[x][1] += dp[i][0] ; } } } int main() { while(scanf("%d", &n) != EOF) { int a,b,root; memset(vis, 0, sizeof(vis)); memset(fa, 0, sizeof(fa)); for(int i = 1;i <= n;i ++) scanf("%d", &rat[i]); for(int i = 1;i <= n-1;i ++) { scanf("%d%d", &a, &b); fa[a] = b; } scanf("%d%d", &a, &b); for(int i = 1;i <= n;i ++) { if(fa[i] == 0) root = i; dp[i][0] = 0; dp[i][1] = rat[i]; } dfs(root); printf("%d\n",max(dp[root][0],dp[root][1])); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/mowenwen_/article/details/47783567