[Knowledge] 4.2.3 Advanced Model of Dynamic Programming——Tree / DAG / Digital DP

Foreword

This article mainly introduces the centralized advanced model of dynamic programming-tree DP, DP on DAG, digital DP. One point that is different from the previous basic models is that their states are not simply based on linear relationships or two-dimensional spaces, but are transferred on tree structures, graphs, and binary digits, which may be more troublesome, but in fact Change soup without changing medicine.

List of subdirectories

1. Tree DP

2. DP on DAG

3. Digital DP

 

4.2.3 Tree / DAG / Digital DP

1. Tree DP

As the name suggests, dynamic programming on the tree structure, there are not many concepts at all, directly introduced with a classic example.

[Prom without a boss] A university has n  employees, numbered 1 ~ n  . There is a subordinate relationship between them, which means that their relationship is like a tree rooted at the principal, and the parent node is the direct superior of the child node. There is now an anniversary banquet, and every employee invited to the banquet will increase a certain happiness index ai , but, if the boss of a certain employee comes to the ball, then the employee will not come to the ball anyway. Therefore, please program and calculate which employees are invited to maximize the happiness index, and seek the greatest happiness index.

The problem of finding children in the tree structure is actually quite convenient. All nodes are only connected to their children and the only parent node. Use this property to cut in: suppose that the tree rooted at i is a child tree. If i participates in the dance, Then all its child nodes cannot participate; if it does not participate, the child node may participate or not; then recursively to all its son nodes, and so on.

State definition: f [i] means "the maximum happiness index that can be obtained by the subtree with i as the root node";

Since there are two states for any node—participation and non-participation, one-dimensional 0/1 is also needed to indicate whether to participate in the dance.

State transition: f [i] [0] = ∑max (f [j] [0], f [j] [1]);

     f [i] [1] = ∑f [j] [0] + a [i] , j represents the son node of i.

Code:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define MAXN 6005
 5 
 6 struct Edge {
 7     int v, nxt;
 8 } e[MAXN];
 9 
10 int n, o, u, v, tot, f[MAXN][2], nr[MAXN], h[MAXN];
11 
12 void addEdge(int u, int v) {
13     e[++o] = (Edge) {v, h[u]}, h[u] = o;
14 }
15 
16 void dfs(int o, int fa) {
17     for (int x = h[o]; x; x = e[x].nxt) {
18         int v = e[x].v;
19         if (v == fa) continue;
20         dfs(v, o);
21         f[o][1] += f[v][0];
22         f[o][0] += max(f[v][0], f[v][1]);
23     }
24 }
25 
26 int main() {
27     cin >> n;
28     for (int i = 1; i <= n; i++) cin >> f[i][1];
29     for (int i = 1; i <= n; i++)
30         cin >> u >> v, addEdge(v, u), nr[u] = 1;
31     for (int i = 1; i <= n; i++) {
32         if (nr[i]) continue;
33         dfs(i, 0);
34         cout << max(f[i][0], f[i][1]);
35         break;
36     }
37      return  0 ;
38 }

2. DP on DAG

 

3. Digital DP

Guess you like

Origin www.cnblogs.com/jinkun113/p/12723563.html