HDU 1520 - Anniversary party
treedp, 选取节点使得不能具有父子节点的同时被选中并统计满足条件树的rating和最大值。
#include <cstdio> using namespace std; #define max(a,b) ((a)>(b)?(a):(b)) int rating[6005]; int tree[6005][6005], N; int dp[6005][2]; void dfs(int n) { dp[n][0] = 0, dp[n][1] = rating[n]; for(int i=1; i<=tree[n][0]; ++i) { int child = tree[n][i]; dfs(child); dp[n][0] += max(dp[child][1], dp[child][0]), dp[n][1] += dp[child][0]; } } int main(void) { while(scanf("%d", &N) > 0) { int i; for(i=0; i<N; ++i) { scanf("%d", &rating[i]); tree[i][0] = 0; } int root = 0; for(int L, K; scanf("%d%d", &L, &K), L || K; ) { if ((tree[K-1][++tree[K-1][0]] = L-1) == root) root = K-1; } dfs(root); printf("%d\n", max(dp[root][0], dp[root][1])); } return 0; }
This article is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
本文采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。