DP on a tree a few introductory topics
Today catching tree DP, feeling starfish.
In fact, quite simple.
Introduce a few examples, I will.
1. Luo Gu P1352 no boss dance
me a solution to a problem
we can consider each node are two cases.
One is to be invited; the other is not going to be invited.
The former result is a child node can not be selected;
the latter is the result of child nodes may be selected.
So clearly in the state transition equation:
DP [the root] [0] STD + :: = max (DP [Son [the root] [I]] [0], DP [Son [the root] [I]] [. 1] );
DP [the root] [. 1] + DP = [Son [the root] [I]] [0];
starfish.
son [root] [i] is the son of the current node.
Initialization to remember is that every point of happiness index.
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#define Max 6050
#define re register
std::vector<int>son[Max];
int n,root,dp[Max][2],fa[Max];
void dfs(int root) {
for(re int i = 0 ; i < son[root].size() ; ++ i)
dfs(son[root][i]);
for(re int i = 0 ; i < son[root].size() ; ++ i) {
dp[root][0] += std::max(dp[son[root][i]][0],dp[son[root][i]][1]);
dp[root][1] += dp[son[root][i]][0];
}
}
void init() {
scanf("%d",&n);int u,v;memset(fa,-1,sizeof fa);
for(re int i = 1 ; i <= n ; ++ i) scanf("%d",&dp[i][1]);
for(re int i = 1 ; i < n ; ++ i)
scanf("%d%d",&u,&v),fa[u]=v,son[v].push_back(u);
scanf("%d%d",&u,&v);
}
inline void print(int root) {printf("%d",std::max(dp[root][0],dp[root][1]));}
void work() {
int root=1;
while(fa[root] != -1) root = fa[root];
dfs(root);
print(root);
}
int main() {
init();
work();
return 0;
}
2.poj1463 Strategic game
we consider each node has two cases.
A is selected; the other is not selected.
The former is a result of his child node may be selected, or may not be selected;
the latter the result is that he must keep a child node selected.
So out of state transition equation:
DP [the root] [0] STD + :: = max (DP [Son [the root] [I]] [0], DP [Son [the root] [I]] [. 1]);
dp [root] [1] + = dp [son [root] [i]] [0];
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#define Max 1550
#define re register
int n,dp[Max][2],fa[Max];
std::vector<int>son[Max];
void dfs(int root) {
for(re int i = 0 ; i < son[root].size() ; ++ i)
dfs(son[root][i]);
for(re int i = 0 ; i < son[root].size() ; ++ i) {
dp[root][0] += dp[son[root][i]][1];
dp[root][1] += std::min(dp[son[root][i]][0],dp[son[root][i]][1]);
}
}
void print(int root) {printf("%d\n",std::min(dp[root][0],dp[root][1]));}
void work() {
int root=0;
while(fa[root] != -1) root = fa[root];
dfs(root);
print(root);
}
void init() {
int k,x,y;char ch;
while(scanf("%d",&n) != EOF) {
for(re int i = 0 ; i < n ; ++ i)
fa[i] = -1,dp[i][0] = 0,dp[i][1] = 1;
for(re int p = 1 ; p <= n ; ++ p) {
scanf("%d",&k);std::cin >> ch;
std::cin >> ch;scanf("%d",&y);
std::cin >> ch;son[k].clear();
for(re int i = 1 ; i <= y ; ++ i)
scanf("%d",&x),fa[x]=k,son[k].push_back(x);
}
work();
}
}
int main() {
init();
return 0;
}