#include<bits/stdc++.h>
typedef int int32;
#define int long long
using namespace std;
const int N = 2e5 + 5;
int n, ans = INT_MIN, dp[N];
vector<int>nbr[N];
void dfs(int x, int fa)
{
for (auto& nxt : nbr[x])
if (nxt != fa)
{
dfs(nxt, x);
dp[x] = max(dp[x], dp[x] + dp[nxt]);
}
ans = max(ans, dp[x]);
return;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> dp[i];
for (int i = 1; i < n; i++)
{
int x, y;
cin >> x >> y;
nbr[x].push_back(y);
nbr[y].push_back(x);
}
dfs(1, 0);
cout << ans;
return 0;
}
#include<bits/stdc++.h>
typedef int int32;
#define int long long
using namespace std;
const int N = 2e5 + 5;
int n, ans, dp[N];
vector<int>nbr[N];
void dfs(int x, int fa)
{
for (auto& nxt : nbr[x])
if (nxt != fa)
{
dfs(nxt, x);
dp[x] = max(dp[x], dp[x] + dp[nxt]);
}
ans = max(ans, dp[x]);
return;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> dp[i];
for (int i = 1; i < n; i++)
{
int x, y;
cin >> x >> y;
nbr[x].push_back(y);
nbr[y].push_back(x);
}
dfs(1, 0);
cout << ans;
return 0;
}
洛谷P1352
P1352 没有上司的舞会
初始值:$\displaystyle dp _ {i,1}=r_i
状态转移见代码。
答案:\displaystyle\max(0,\max dp _ {i,0/1})。
这道题需要找根节点!
#include<bits/stdc++.h>
typedef int int32;
#define int long long
using namespace std;
const int N = 2e5 + 5;
int n, root, ans, dp[N][2], in[N];
vector<int>nbr[N];
void dfs(int x, int fa)
{
for (auto& nxt : nbr[x])
if (nxt != fa)
{
dfs(nxt, x);
dp[x][0] = max({ dp[x][0],dp[x][0] + dp[nxt][1],dp[x][0] + dp[nxt][0] });
dp[x][1] = max(dp[x][1], dp[x][1] + dp[nxt][0]);
}
ans = max({ ans,dp[x][0],dp[x][1] });
return;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> dp[i][1];
for (int i = 1; i < n; i++)
{
int x, y;
cin >> x >> y;
nbr[x].push_back(y);
nbr[y].push_back(x);
in[x]++;
}
for (int i = 1; i <= n; i++)
if (!in[i])
{
root = i;
break;
}
dfs(root, 0);
cout << ans;
return 0;
}
洛谷P2016
P2016 战略游戏
初始值:$\displaystyle dp _ {i,1}=1
状态转移见代码。
答案:\displaystyle\min(dp _ {1,0}, dp _ {1,1})。
#include<bits/stdc++.h>
typedef int int32;
#define int long long
using namespace std;
const int N = 2e5 + 5;
int n, dp[N][2];
vector<int>nbr[N];
void dfs(int x, int fa)
{
for (auto& nxt : nbr[x])
if (nxt != fa)
{
dfs(nxt, x);
dp[x][0] += dp[nxt][1];
dp[x][1] += min(dp[nxt][0], dp[nxt][1]);
}
return;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
{
dp[i][1] = 1;
int x, y, z;
cin >> x >> z;
x++;
while (z--)
{
cin >> y;
y++;
nbr[x].push_back(y);
nbr[y].push_back(x);
}
}
dfs(1, 0);
cout << min(dp[1][0], dp[1][1]);
return 0;
}
洛谷P4084
P4084 [USACO17DEC] Barn Painting G
初始值、状态转移见代码。
答案:$\displaystyle dp _ {1,0} + dp _ {1,1} + dp _ {1,2}$。
```cpp
#include<bits/stdc++.h>
typedef int int32;
#define int long long
using namespace std;
const int N = 1e5 + 5, mod = 1e9 + 7;
int n, m, dp[N][3], a[N], ans;
vector<int>nbr[N];
void dfs(int x, int fa)
{
if (a[x])
dp[x][--a[x]] = 1;
else
for (int i = 0; i < 3; i++)
dp[x][i] = 1;
for (auto& nxt : nbr[x])
if (nxt != fa)
{
dfs(nxt, x);
for (int i = 0; i < 3; i++)
(dp[x][i] *= (dp[nxt][0] + dp[nxt][1] + dp[nxt][2] -
dp[nxt][i]) % mod) %= mod;
}
return;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i < n; i++)
{
int x, y;
cin >> x >> y;
nbr[x].push_back(y);
nbr[y].push_back(x);
}
for (int i = 1; i <= m; i++)
{
int x;
cin >> x;
cin >> a[x];
}
dfs(1, 0);
cout << (dp[1][0] + dp[1][1] + dp[1][2]) % mod;
return 0;
}
```
## 第二类树形DP
兄弟节点之间有数量约束关系(背包形)。
### 例题
#### 洛谷P2015
[P2015 二叉苹果树](https://www.luogu.com.cn/problem/P2015)
我们考虑加强版:是多叉树。
一条边被保留,当且仅当父亲节点被保留,考虑DP。
$dp _ {i,j}$ 表示以 $i$ 为根节点,保留 $j$ 条边的最多苹果数。
答案为 $dp_{1,Q}$。
```cpp
#include<bits/stdc++.h>
typedef int int32;
#define int long long
using namespace std;
const int N = 3e4 + 5;
int n, m, dp[105][N], size[N];
vector<pair<int, int>>nbr[N];
void dfs(int x, int fa)
{
for (auto& [nxt, w] : nbr[x])
if (nxt != fa)
{
dfs(nxt, x);
::size[x] += ::size[nxt] + 1;
for (int i = min(::size[x], m); i >= 0; i--)
for (int j = 0; j <= min(::size[nxt], i - 1); j++)
dp[x][i] = max(dp[x][i], dp[nxt][j] + dp[x][i - j - 1] + w);
}
return;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i < n; i++)
{
int x, y, z;
cin >> x >> y >> z;
nbr[x].push_back({ y,z });
nbr[y].push_back({ x,z });
}
dfs(1, 0);
cout << dp[1][m];
return 0;
}
```
#### 洛谷P2014
[P2014 [CTSC1997] 选课 - 洛谷](https://www.luogu.com.cn/problem/P2014)
这道题可以转化为上一题。
把 $0$ 当做根节点,点权转化为连接父节点的边权。
```cpp
#include<bits/stdc++.h>
typedef int int32;
#define int long long
using namespace std;
const int N = 6e3 + 5;
int n, m, dp[305][N], size[N];
vector<pair<int, int>>nbr[N];
void dfs(int x, int fa)
{
for (auto& [nxt, w] : nbr[x])
if (nxt != fa)
{
dfs(nxt, x);
::size[x] += ::size[nxt] + 1;
for (int i = min(::size[x], m); i >= 0; i--)
for (int j = 0; j <= min(::size[nxt], i - 1); j++)
dp[x][i] = max(dp[x][i], dp[nxt][j] + dp[x][i - j - 1] + w);
}
return;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
int x = i, y, z;
cin >> y >> z;
nbr[x].push_back({ y,z });
nbr[y].push_back({ x,z });
}
dfs(0, 0);
cout << dp[0][m];
return 0;
}
```
以上两题时间复杂度为 $O(n^2)$。
证明:
> 状态转移的过程,是两个背包合并的过程。
>
> 计算每个点被合并的总次数,即可求时间复杂度。
>
> 设 $T(i)$ 表示点 $i$ 被合并的次数,则:
>
> $$\displaystyle\begin{aligned}T(i)&=1+(1+size_1)size_2+\left(1+\sum _ {j=1}^2 size _ j\right)size_3+\cdots+\left(1+\sum _ {j=1}^k size _ j\right)\\&=\frac{\displaystyle\left(1+\sum_{i=1}^k size_i\right)^2-\sum size_j^2}{2}+1\\&=\frac{\displaystyle size_i^2-\sum size_j^2}{2}+1\end{aligned}$$
>
> 总时间:
>
> $$\displaystyle\begin{aligned}\sum _ {i=1}^n T(i)&=\frac{size_1^2+n}{2}+1\\&\approx n^2\end{aligned}$$
#### 洛谷P1273
[P1273 有线电视网](https://www.luogu.com.cn/problem/P1273)
$dp _ {i,j}$ 表示以 $i$ 为根节点,保留 $j$ 个用户的最大利润。
$dp_{i,0}=0,dp_{i,1}=w_i$,其余极小值。
答案需要枚举。
```cpp
#include<bits/stdc++.h>
typedef int int32;
#define int long long
using namespace std;
const int N = 3e3 + 5;
int n, m, dp[N][N], size[N];
vector<pair<int, int>>nbr[N];
void dfs(int x, int fa)
{
for (auto& [nxt, w] : nbr[x])
if (nxt != fa)
{
dfs(nxt, x);
::size[x] += ::size[nxt];
for (int i = min(::size[x], m); i >= 0; i--)
for (int j = 0; j <= min(::size[nxt], i); j++)
dp[x][i] = max(dp[x][i], dp[nxt][j] + dp[x][i - j] - w);
}
return;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m;
memset(dp, 0xcf, sizeof dp);
for (int i = 1; i <= n; i++)
dp[i][0] = 0;
for (int i = 1; i <= n - m; i++)
{
int tmp;
cin >> tmp;
while (tmp--)
{
int x = i, y, z;
cin >> y >> z;
nbr[x].push_back({ y,z });
}
}
for (int i = n - m + 1; i <= n; i++)
cin >> dp[i][1], ::size[i] = 1;
dfs(1, 0);
for (int i = m; i >= 0; i--)
if (dp[1][i] >= 0)
{
cout << i;
return 0;
}
return 0;
}
```
#### 洛谷P1272
[P1272 重建道路](https://www.luogu.com.cn/problem/P1272)
$dp _ {i,j}$ 表示以 $i$ 为根节点,分离出 $j$ 个节点的子树的道路的最小数目。
$dp_{i,1}=\text{儿子节点数}$,其余极大值。
答案需要枚举。
```cpp
#include<bits/stdc++.h>
typedef int int32;
#define int long long
using namespace std;
const int N = 3e3 + 5;
int n, m, dp[N][N], size[N], ans = INT_MAX;
vector<pair<int, int>>nbr[N];
void dfs(int x, int fa)
{
::size[x] = 1;
for (auto& [nxt, w] : nbr[x])
if (nxt != fa)
{
dfs(nxt, x);
::size[x] += ::size[nxt];
for (int i = min(::size[x], m); i >= 1; i--)
for (int j = 1; j <= min(::size[nxt], i - 1); j++)
dp[x][i] = min(dp[x][i], dp[nxt][j] + dp[x][i - j] - 1);
}
return;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m;
memset(dp, 0x3f, sizeof dp);
for (int i = 1; i < n; i++)
{
int x, y, z = 1;
cin >> x >> y;
nbr[x].push_back({ y,z });
}
for (int i = 1; i <= n; i++)
dp[i][1] = nbr[i].size();
dfs(1, 0);
for (int i = 2; i <= n; i++)
ans = min(ans, dp[i][m] + 1);
cout << min(ans, dp[1][m]);
return 0;
}
```