Tree-shaped dp explanation (you won’t regret point in)

I hope you have a certain understanding of the basic dp before reading this article, and you can understand this explanation even if you don’t learn well.

Let's start with an example question:

Tree-shaped dp well-known entry example: no boss’s
link to the ball topic
Insert picture description here
Tree-shaped dp is a tree-based dp (I don’t need to say a tree, it doesn’t matter whether it is data structure or discrete mathematics, it seems to have to learn)
Here we talk about how Save the tree

#include<iostream>
#include<math.h>

using namespace std;
const int N=6000+10;
int last[N],ne[N],edge[N],cnt;
bool biao[N];
void add(int a, int b){
    
    
    edge[cnt] = b;
    ne[cnt] = last[a];
    last[a] = cnt++;
}

int main()

{
    
    
  int n;
  cin>>n;//n名员工
  int a,b;
  for(int i=1;i<n;i++)//n-1条边
  {
    
    cin>>a>>b;//b是a的直接上司
    add(a,b);
    
  
    
  }
  
    
}

Let's simulate it, suppose there is such a tree.
Insert picture description here
Suppose we need to find the child node of 1
through the last[a] array, we can get a cnt, through this cnt, we can get a child node b of dge[cnt] = b array

void add(int a, int b){
    
    
    edge[cnt] = b;
    ne[cnt] = last[a];
    last[a] = cnt++;
}

Note that this code cnt++ is executed last.
In fact, we can also write this

void add(int a, int b){
    
    
    cnt++//此时cnt是从一开始,遍历时需要注意一下
    edge[cnt] = b;
    ne[cnt] = last[a];
    last[a] = cnt;
}

We can even write like this

void add(int a, int b){
    
    
    
    edge[cnt] = b;
    ne[cnt] = last[a];
    last[a] = cnt;
    cnt++}

We notice that when the value of last cnt is served to last[a], the value of last[a] is the value of cnt when a was the parent node last time, and we have already changed last[a] to the value of cnt at this time It is assigned to ne[cnt] and this cnt is assigned to last[a]; to
summarize: get cnt through last[a], get all the children through edge[cnt], and get it through ne[cnt] The cnt when a was the parent node last time, and so on

Learn achievements, we can start to look at this question of;
or is this lesson tree

Insert picture description here
Suppose we choose 1, the maximum value to be sought is the maximum value when the left subtree does not choose 2 plus the maximum value when the right subtree does not choose 3, and the weight of 1 is added at the end;
suppose we do not choose 1, what is required The maximum value is max (select 2 for the left subtree, not select 2 for the left subtree) + max (select 3 for the right subtree, and not select 3 for the right subtree);
you get the maximum value of node 1, and all nodes can be based on this The equation finds the maximum value of this state;
let’s take a look at the code below:
c++ code

#include<iostream>
#include<math.h>
#include<string.h>
using namespace std;
const int N=6000+10;
int last[N];
int ne[N],edge[N],cnt=1;
bool biao[N];
void add(int a, int b){
    
    
    edge[cnt] = b;
    ne[cnt] = last[a];
    last[a] = cnt++;
}
int dp[N][2];

int a[N];
void dps(int root)
{
    
    

   dp[root][0]=0;//不选root号节点;
   dp[root][1]=a[root];//选root号节点


   for(int i=last[root];i>=1;i=ne[i])
   {
    
    
       int j=edge[i];
       dps(j);
       dp[root][0]+=max(dp[j][0],dp[j][1]);
       dp[root][1]+=dp[j][0];
   }


}



int main()

{
    
    
  int n;
  cin>>n;
  for(int i=1;i<=n;i++)
    cin>>a[i];
  //因为0包含的有边
  int x,y;
  
  for(int i=1;i<n;i++)//n-1条边
  {
    
    cin>>x>>y;//y是x的直接上司
    add(y,x);


    biao[x]=true;//a有父节点b
  }
  int v=1;
  while(biao[v])v++;//找到没有父节点的点
  dps(v);


    cout<<max(dp[v][1],dp[v][0]);
}

java code:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Main{
    
    
    static int N = 6010;
    static int[] happy = new int[N];
    static int[] h = new int[N];
    static int[] e = new int[N];
    static int[] ne = new int[N];
    static int idx = 0;
    static int[][] f = new int[N][2];
    static boolean[] has_fa = new boolean[N];
    static void add(int a,int b)
    {
    
    
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx ++;
    }
    static void dfs(int u)
    {
    
    
        f[u][1] = happy[u];
        for(int i = h[u]; i != -1;i = ne[i])
        {
    
    
            int j = e[i];
            dfs(j);

            f[u][1] += f[j][0];
            f[u][0] += Math.max(f[j][1], f[j][0]);
        }
    }
    public static void main(String[] args) {
    
    
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        for(int i = 1;i <= n;i ++) happy[i] = scan.nextInt();
        Arrays.fill(h, -1);
        for(int i = 0;i < n - 1;i ++)
        {
    
    
            int a = scan.nextInt();
            int b = scan.nextInt();
            add(b,a);
            has_fa[a] = true;
        }
        int root = 1;
        while(has_fa[root]) root ++;
        dfs(root);

        System.out.println(Math.max(f[root][0], f[root][1]));
    }
}


In the same way, let’s take a look at this question:
title link
Insert picture description here

#include<iostream>
#include<cstring>
using namespace std;
typedef long long LL;
const int N = 100010,M=N*2;
int h[N],e[M],ne[M],w[N],idx;
int n;
LL f[N];
void add(int a,int b)
{
    
    
    e[idx]=b;ne[idx]=h[a];h[a]=idx++;
}
void dfs(int u,int father)
{
    
    
    f[u]=w[u];
    for(int i=h[u];~i;i=ne[i])
    {
    
    
        int j = e[i];
        if(j!=father)//不能让它倒回去
        {
    
    
            dfs(j,u);
            f[u]+=max(0ll,f[j]);
        }
    }
}

int main()
{
    
    
    cin>>n;
    memset(h,-1,sizeof h);
    for(int i=1;i<=n;++i)cin>>w[i];
    for(int i=0;i<n-1;++i){
    
    
        int a,b;
        cin>>a>>b;
        add(a,b);
        add(b,a);
    }
    dfs(2,-1);
    LL res = f[1];
    for(int i=2;i<=n;++i)res = max(res,f[i]);
    cout<<res<<endl;
    return 0;
}



java code:

import java.util.*;

public class Main {
    
    
    private static int n;
    private static long res;
    private static long[] w;
    private static List<Integer>[] g;
    private static long[] f ;

    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        n =sc.nextInt();
        w = new long[n+1];
        g = new ArrayList[n+1];
        f = new long[n+1];

        for(int i = 0;i < n+1; i++){
    
    
            g[i] = new ArrayList<Integer>();
        }

        for(int i = 1;i <= n;i ++){
    
    
             w[i] = sc.nextLong();
        }

        for(int i = 0;i < n - 1;i ++)
        {
    
    
            int a = sc.nextInt();
            int b = sc.nextInt();
            g[a].add(b);
            g[b].add(a);
        }

        dfs(1,0);
        res = f[1];
        for(int i = 2; i < n+1; i++){
    
    
            if(res<f[i]){
    
    
                res = f[i]; 
            }
        }
        System.out.println(res);
    }
    /**
    *root作为根所代表的子树有一个最大权和,将其存储在f[root]中
    */
    private static void dfs(int root,int father){
    
    
        f[root] = w[root];
        for(int i = 0; i < g[root].size(); i++){
    
    
            Integer child = g[root].get(i);
            if(child == father){
    
    
                continue;
            }
            dfs(child,root);
            if(f[child] > 0){
    
    
                f[root] += f[child];
            }
        }
    }    
}




Guess you like

Origin blog.csdn.net/jahup/article/details/107203550