【浮*光】#树形DP# 树形DP的习题集
T1:【p2996】拜访奶牛 树的相邻节点不能选择,求最多选择的节点数。 【0/1型树形dp】← 也只有我这样叫... 这题是真的很模板... f[x] 即 拜访x时最大数量,g[x] 即 不拜访x时最大数量。 转移方程:f[x]=1+∑g[son[i]],g[x]=∑max(f[son[i]],g[son[i]])。 不妨假设从1号点出发,那么答案即为max(f[1],g[1])。 #include<iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < string > #include <queue> #include <map> #include <vector> #include <cmath> using namespace std; typedef long long ll; /* 【p2996】拜访奶牛 N(1<=N<=50000)个朋友构成一棵树。求:可以拜访的朋友的最大数目。 限制:对于由一条路直接相连的两个奶牛,只能拜访其中的一个。 */ /* 【0/1型树形dp】← 也只有我这样叫... f[x] 即 拜访x时最大数量,g[x] 即 不拜访x时最大数量。 转移方程:f[x]=1+∑g[son[i]],g[x]=∑max(f[son[i]],g[son[i]]