树形dp and all of dp what we are learning now

· · 算法·理论

基础

树形dp就是在树上做dp

all dp

Code

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 105;
const int NN = 1005;
const int NNN = 10000005;
int n,val[16005],dp[16005];
vector<int> g[16005];
void dfs(int x,int fa){
    dp[x] = val[x];
    for(auto it:g[x]){
        if(it == fa)continue;
        dfs(it,x);
        dp[x] += max(0ll,dp[it]);
    }
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for(int i = 1;i <= n;i++){
        cin >> val[i];
    }
    for(int i = 1;i < n;i++){
        int u,v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(1,0);
    int ans = -1e18;
    for(int i = 1;i <= n;i++){
        ans = max(ans,dp[i]);
    }
    cout << ans;
    return 0;
}

P1352 没有上司的舞会

还是一样的,题目怎么要求就怎么设状态,所以我们设 dp_u 为以 u 为根时的最大快乐值。

可是,我们这样设状态无法知道当前是否选,所以无法进行接下来的转移,所以我们要多加一维:dp_{u , 0} 表示不选,dp_{u , 1} 表示选。

那么答案就是 max(dp_{i , 0} , dp_{i , 1})。

如果选了当前的点,那么子节点就不能选,所以选的转移是:dp_{u , 1} \gets dp_{u , 1} + dp_{v , 0};如果当前的点不选,那么子节点可以选也可以不选,所以不选的转移是:dp_{u , 0} \gets dp_{u , 0} + max(dp_{v , 0} , dp_{v , 1})。

如果当前的点不选,初始值就是 dp_{u , 0} = 0,不选就没有贡献,如果选,初始值就是 dp_{u , 1} = r_u,选的话就是自己产生的贡献。

Code

#include<bits/stdc++.h>
#define ll long long
#define Code using
#define by namespace
#define cmy std
#define un unsigned
#define problem signed
#define T1 main
#define PII pair<int, int>
#define fi first
#define se second
#define un_m unorderd_map
#define un_s unorderd_set
Code by cmy;
const int N = 16005;
const int M = 100005;
const int INF = 1e9;
const int mod = 998244353;
int n , ans ;
int a[N] , dp[N][2] ;
vector<int> G[N] ; 
void dfs(int u , int fa){
    for(int i = 0; i < G[u].size(); i++){
        if(G[u][i] == fa) continue;
        dfs(G[u][i], u);
        dp[u][1] = max(dp[u][1] , dp[u][1] + dp[G[u][i]][0]);
        dp[u][0] = max(dp[u][0] , max(dp[u][0] + dp[G[u][i]][0] , dp[u][0] + dp[G[u][i]][1]));
    }
}
void work(){
    //please write your code here
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i < n; i++){
        int u, v;
        cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    for(int i = 1; i <= n; i++) dp[i][1] = a[i];
    dfs(1 , -1);
    cout << max(dp[1][1], dp[1][0]);
}
problem T1(){
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1; while(t--){work();}
    return 0;
}

P2016 战略游戏

首先输入需要特殊注意一下,而且编号是从 0 开始的。

然后,我们定义 dp_{u , 1} 为 u 放士兵,dp_{u , 0} 为不放士兵。

答案就是 min({dp_{1 , 0} , dp_{1 , 1}})。

那么转移就很好写了,如果当前的点不放,那么子节点一定得放;如果放了,就不一定要放。

dp_{u , 0} \gets dp_{v , 1} \\ dp_{u , 1} \gets min(dp_{v , 0} , dp_{v , 1})

初始值的话,如果当前放士兵,就是 1,否则为 0。

Code

#include<bits/stdc++.h>
#define ll long long
#define Code using
#define by namespace
#define cmy std
#define un unsigned
#define problem signed
#define T1 main
#define PII pair<int, int>
#define fi first
#define se second
#define un_m unorderd_map
#define un_s unorderd_set
Code by cmy;
const int N = 16005;
const int M = 100005;
const int INF = 1e9;
const int mod = 998244353;
int n , ans ;
int a[N] , dp[N][2] ;
vector<int> G[N] ; 
void dfs(int u , int fa){
    for(int i = 0; i < G[u].size(); i++){
        if(G[u][i] == fa) continue;
        dfs(G[u][i], u);
        dp[u][0] += dp[G[u][i]][1];
        dp[u][1] += min(dp[G[u][i]][0] , dp[G[u][i]][1]);
    }
}
void work(){
    //please write your code here
    cin >> n;
    for(int i = 1; i <= n; i++){
        int u, k;
        cin >> u >> k;
        u++;
        for(int j = 1; j <= k; j++){
            int v;
            cin >> v;
            v++;
            G[u].push_back(v);
            G[v].push_back(u);
        }
    }
    for(int i = 1; i <= n; i++) dp[i][1] = 1;
    dfs(1 , -1);
    cout << min(dp[1][1], dp[1][0]);
}
problem T1(){
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1; while(t--){work();}
    return 0;
}

P2585 [ZJOI2006] 三色二叉树

这题的输入比上一题还毒瘤。

我们定义 dp_{u , 0} 为当前节点为绿色,剩下的分别为红色和蓝色。

那么我们当前的点要从两个子节点转移,因为不能填相同的,所以我们要从另外两个和当前颜色不同的转移过来,注意有两种顺序。

然后题目要求最大值和最小值,复制一遍即可。

初始值的话,如果当前的是绿色,那么答案就是 1,否则是 0。

Code

#include<bits/stdc++.h>
#define ll long long
#define Code using
#define by namespace
#define cmy std
#define un unsigned
#define problem signed
#define T1 main
#define PII pair<int, int>
#define fi first
#define se second
#define un_m unorderd_map
#define un_s unorderd_set
Code by cmy;
const int N = 5e5 + 10;
const int M = 100005;
const int INF = 1e9;
const int mod = 998244353;
int n,m,d[N],f[N][3],f1[N][3],cnt = 1;
string s;
void dfs(int x){
    if(s[x] == '0'){
        f[x][0] = f1[x][0] = 1;
        return;
    }
    ++cnt;
    dfs(cnt);
    if(s[x] == '1'){
        f[x][0] = max(f[x + 1][1], f[x + 1][2]) + 1;
        f[x][1] = max(f[x + 1][0], f[x + 1][2]);
        f[x][2] = max(f[x + 1][0], f[x + 1][1]);
        f1[x][0] = min(f1[x + 1][1], f1[x + 1][2]) + 1;
        f1[x][1] = min(f1[x + 1][0], f1[x + 1][2]);
        f1[x][2] = min(f1[x + 1][0], f1[x + 1][1]);
    }else{
        ++cnt;
        int y = cnt;
        dfs(cnt);
        f[x][0] = max(f[x + 1][1] + f[y][2], f[x + 1][2] + f[y][1]) + 1;
        f[x][1] = max(f[x + 1][0] + f[y][2], f[x + 1][2] + f[y][0]);
        f[x][2] = max(f[x + 1][0] + f[y][1], f[x + 1][1] + f[y][0]);
        f1[x][0] = min(f1[x + 1][1] + f1[y][2], f1[x + 1][2] + f1[y][1]) + 1;
        f1[x][1] = min(f1[x + 1][0] + f1[y][2], f1[x + 1][2] + f1[y][0]);
        f1[x][2] = min(f1[x  +  1][0]  +  f1[y][1], f1[x  +  1][1]  +  f1[y][0]);
    }
    return;
}
void work(){
    //please write your code here
    cin >> s;
    s = " " + s;
    dfs(1);
    cout << max({f[1][0], f[1][1], f[1][2]}) << ' ' << min({f1[1][0], f1[1][1], f1[1][2]});
}
problem T1(){
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1; while(t--){work();}
    return 0;
}
The End.
\ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\
\textcolor{#FE4C61}{\mathrm{r}}\textcolor{#F39C11}{\mathrm{a}}\textcolor{FFC116}{\mathrm{i}}\textcolor{#52C41A}{\mathrm{n}}\textcolor{#3498DB}{\mathrm{b}}\textcolor{#9D3DCF}{\mathrm{o}}\textcolor{#0E1D69}{\mathrm{w}}\ \,\textcolor{#52C41A}{\mathrm{e}}\textcolor{#E74C3C}{\mathrm{g}}\textcolor{#9D3DCF}{\mathrm{g}}

军训好难,好苦呀