树形DP模型与题目详解acm


#写在前面

##没有上司的舞会

https://www.acwing.com/problem/content/287/
这里用了状态机

----c++版

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
//有两种状态 f[u,0]: 所有从以u为根的子树中选择,并且不选u这个点的方案
//           f[u,1]: 所有从以u为根的子树中选择,并且选u这个点的方案 
//属性:max
//计算:f[u,0]=∑max(f[si,0],f[si,1]);
//      f[u,1]=∑f[si,0];
//o(n)
const int N=6000;
int n;
int happy[N];
int h[N],e[N],ne[N],idx;
int f[N][2];
bool has_father[N];//这题需要自己看一下是否有根节点

void add(int a,int b){
    e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}

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][0]+=max(f[j][0],f[j][1]);
        f[u][1]+=f[j][0];
    }
}

int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&happy[i]);
    
    memset(h, -1, sizeof h);
    for(int i=0;i<n-1;i++){//n个点的数最大有n-1条边
        int a,b;
        scanf("%d%d",&a,&b);
        has_father[a]=true;
        add(b,a);
    }
    
    int root=1;
    while(has_father[root])root++;
    
    dfs(root);
    
    printf("%d", max(f[root][0],f[root][1]));
    
    return 0;
}

##树的最长路径

https://www.acwing.com/problem/content/1074/

经典找树的直径
在这里插入图片描述
在这里插入图片描述
如果边有负的权值,则需要树形dp
想办法把所有边枚举一遍,选出最大的

在这里插入图片描述
用每个状态dfs表示所有挂在当前点上的路径的最大值?

----c++版

//树的最长路径也可以称为树的直径
#include<iostream>
#include<algorithm>
using namespace std;
#include<cstring>
const int N=10010, M=N*2;
int n;
int h[N], e[M], w[M], ne[M], idx;
int ans;

void add(int a, int b, int c){
    e[idx]=b, w[idx]=c, ne[idx]=h[a], h[a]=idx++;
}

int dfs(int u, int father){
    int dist = 0;//表示从当前点往下走的最大长度
    int d1=0, d2=0;//注意最长距离和次长距离都初始化成0就可以了,如果有负数的边就当它不存在
    //d, d1, d2都是用来求出最大值和次大值
    
    for(int i=h[u]; i!=-1; i=ne[i]){
        int j=e[i];
        if(j==father)continue;//由于是无向图,所以可能往回走
        int d=dfs(j, u)+w[i];
        dist = max(dist, d);// dp 的体现 ?
        
        if(d>=d1)d2=d1,d1=d;
        else if (d>d2)d2=d;
    }
    ans=max(ans, d1+d2);//每一个点的 d1+d2 都可能是答案
    return dist;
}

int main(){
    cin>>n;
    memset(h, -1, sizeof h);
    for(int i=0;i<n-1;i++){
        int a,b,c;
        cin>>a>>b>>c;
        add(a, b, c);
        add(b, a, c);
    }
    
    dfs(1, -1);//dfs不能往上走,不然就死循环了
    
    cout<<ans<<endl;
    return 0;
}

##树的中心

https://www.acwing.com/problem/content/1075/
在这里插入图片描述
向上走就是求一个点的父节点的不走该节点的最长路径,其实我们知道了每一个节点向下走的长度就可以知道向上的最长路径了,一个子节点 j 的向上最长路径就是 它的父节点 u 的最长向上路径和最长向下路径取最大值,如果向下最长路径经过了 j 就改为第二长的向下路径

----c++版

//直观上看起来有点像中心
#include<iostream>
#include<algorithm>
using namespace std;
#include<cstring>
//这题也是暴力枚举,只不过我们用dp的方式让我们枚举得稍微快一些
const int N=10010, M=N*2, inf=0x3f3f3f3f;
int n;
int h[N], e[M], w[M], ne[M], idx;
int d1[N], d2[N], up[N];
int p1[N], p2[N];

void add(int a, int b, int c){
    e[idx]=b, w[idx]=c, ne[idx]=h[a], h[a]=idx++;
}

int dfs_d(int u, int father){
    d1[u]=d2[u]=-inf;
    for(int i=h[u]; i!=-1; i=ne[i]){
        int j=e[i];
        if(j==father)continue;
        int d=dfs_d(j, u)+w[i];
        if(d>d1[u]){
            d2[u]=d1[u]; d1[u]=d;
            p2[u]=p1[u];p1[u]=j;
        }
        else if(d > d2[u]){
            d2[u]=d;
            p2[u]=j;
        }
    }
    if(d1[u]==-inf)d1[u]=d2[u]=0;
    return d1[u];
}

void dfs_u(int u, int father){//每下一个点的父亲的up都是提前算过的,这个是精髓啊
    for(int i=h[u]; i!=-1; i=ne[i]){
        int j=e[i];
        if(j==father)continue;
        if(p1[u]==j)up[j]=max(up[u], d2[u])+w[i];
        else up[j]=max(up[u], d1[u])+w[i];
        
        dfs_u(j, u);
    }
}

int main(){
    cin>>n;
    memset(h, -1, sizeof h);
    for(int i=0; i<n-1; i++){
        int a,b,c;
        cin>>a>>b>>c;
        add(a,b,c);add(b,a,c);
    }
    
    dfs_d(1, -1);
    dfs_u(1, -1);
    
    int res=inf;
    for(int i=1;i<=n;i++)res=min(res, max(d1[i], up[i]));
    
    printf("%d\n", res);
    
    return 0;
}

##数字转换

https://www.acwing.com/problem/content/1077/
在这里插入图片描述

----c++版

#include<iostream>
#include<algorithm>
using namespace std;
const int N=50010;
#include<cstring>
int n;
int h[N], e[N], ne[N], idx;
int sum[N];//存所有数的所有约数之和
int st[N];//因为不只有一棵树,所以需要存一下哪些点是树根
int ans;

void add(int a, int b){
    e[idx]=b; ne[idx]=h[a]; h[a]=idx++;
}

int dfs(int u){
    int d1=0, d2=0;
    for(int i=h[u]; ~i; i=ne[i]){
        int j=e[i];
        int d=dfs(j)+1;
        if(d>=d1)d2=d1, d1=d;
        else if (d>d2)d2=d;
    }
    ans=max(ans, d1+d2);
    return d1;
}

int main(){
    cin>>n;
    for(int i=1; i<=n; i++)
        for(int j=2; j<=n/i; j++)
            sum[i*j]+=i;
    
    memset(h, -1, sizeof h);
    for(int i=2; i<=n; i++)//i需要从2开始枚举,因为不存在0号点,变化的过程中不能有0
        if(i>sum[i]){
            add(sum[i], i);
            st[i]=true;
        }
    
    for(int i=1; i<=n; i++)
        if(!st[i]){//如果是树根
            dfs(i);//变化是单向的,所以是有向边,不用你考虑往上走的问题
        }
    cout<<ans<<endl;
    return 0;
}

##二叉苹果树

https://www.acwing.com/problem/content/1076/

在这里插入图片描述

----c++版

#include<iostream>
#include<algorithm>
using namespace std;
//有依赖背包问题的化简版
#include<cstring>
const int N=110, M=N*2; //虽然有跟节点,但我们不知道边的方向,所以还是建成无相树
int n,m;
int h[N], e[M], w[M], ne[M], idx;
int f[N][N];

void add(int a, int b, int c){
    e[idx]=b, w[idx]=c, ne[idx]=h[a], h[a]=idx++;
}

void dfs(int u, int father){
    for(int i=h[u]; ~i; i=ne[i]){//先物品组
        if(e[i]==father)continue;
        dfs(e[i], u);
        for(int j=m; j>=0; j--)//后体积  必须从大到小
            for(int k=0; k<j; k++)//决策
                f[u][j]=max(f[u][j], f[u][j-k-1]+f[e[i]][k]+w[i]);
    }
}

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

##战略游戏

https://www.acwing.com/problem/content/325/
极大独立集及其相关概念:
https://blog.csdn.net/Richard_for_OI/article/details/79520470

在这里插入图片描述

----c++版

#include<iostream>
#include<algorithm>
using namespace std;
//和没有上司的舞会很像
#include<cstring>
const int N=1510;
int n;
int h[N], e[N], ne[N], idx;
int f[N][2];
bool st[N];

void add(int a, int b){
    e[idx]=b, ne[idx]=h[a], h[a]=idx++;
}

void dfs(int u){
    f[u][0]=0;//每次都要清空
    f[u][1]=1;//有士兵的状态
    for(int i=h[u]; ~i; i=ne[i]){
        int j=e[i];
        dfs(j);
        
        f[u][0]+=f[j][1];
        f[u][1]+=min(f[j][0], f[j][1]);
    }
}

int main(){
    while(scanf("%d", &n)!=-1){
        memset(h, -1, sizeof h);
        idx=0;
        memset(st, 0, sizeof st);
        for(int i=0; i<n; i++){
            int id, cnt;
            scanf("%d:(%d)", &id, &cnt);
            while(cnt--){
                int ver;
                scanf("%d", &ver);
                add(id, ver);
                st[ver]=true;
            }
        }
        int root=0;
        while(st[root])root++;
        dfs(root);
        
        printf("%d\n", min(f[root][0], f[root][1]));
    }
    
    return 0;
}

##皇宫看守

https://www.acwing.com/problem/content/1079/

在这里插入图片描述

----c++版

#include<iostream>
#include<algorithm>
using namespace std;
#include<cstring>
const int N=1510;
int n;
int h[N], e[N], w[N], ne[N], idx;
int f[N][3];
bool st[N];

void add(int a, int b){
    e[idx]=b, ne[idx]=h[a], h[a]=idx++;
}

void dfs(int u){
    f[u][2]=w[u];
    
    for(int i=h[u]; ~i; i=ne[i]){
        int j=e[i];
        dfs(j);
        
        f[u][0]+=min(f[j][1], f[j][2]);
        f[u][2]+=min(min(f[j][0], f[j][1]), f[j][2]);
    }
    
    f[u][1]=1e9;
    for(int i=h[u]; ~i; i=ne[i]){
        int j=e[i];
        f[u][1]=min(f[u][1], f[j][2]+f[u][0]-min(f[j][1], f[j][2]));
    }
}

int main(){
    cin>>n;
    memset(h, -1, sizeof h);
    for(int i=1; i<=n; i++){
        int id, cost, cnt;
        cin>>id>>cost>>cnt;
        w[id]=cost;
        while(cnt--){
            int ver;
            cin>>ver;
            add(id, ver);
            st[ver]=true;//用来找跟节点
        }
    }
    int root=1;
    while(st[root])root++;
    dfs(root);
    cout<<min(f[root][1], f[root][2])<<endl;//根节点没有父节点
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值