这里写自定义目录标题
- 1、树的最长路径(树的直径)
- 2、树的中心
- 3、数字转换(树的最长路径)
- 4、二叉苹果树(完全二叉树 有依赖的背包问题)
- 5、战略游戏
- 6、皇宫看守
1、树的最长路径(树的直径)
![算法提高之动态规划:树形dp_第1张图片](http://img.e-com-net.com/image/info8/645c3fd21d714254b3bcafc58c7c2161.jpg)
![算法提高之动态规划:树形dp_第2张图片](http://img.e-com-net.com/image/info8/e3dd1126899c4a2dbf1bd980e4c8ffdf.jpg)
![算法提高之动态规划:树形dp_第3张图片](http://img.e-com-net.com/image/info8/a7dccadec7a644ff90d6ebee61f8271d.jpg)
![算法提高之动态规划:树形dp_第4张图片](http://img.e-com-net.com/image/info8/03c7e26dbe1e45e88c5aa5befe0b4664.jpg)
![算法提高之动态规划:树形dp_第5张图片](http://img.e-com-net.com/image/info8/e4dce27e36f140138def136c42dbce40.jpg)
![算法提高之动态规划:树形dp_第6张图片](http://img.e-com-net.com/image/info8/c624f9df09f54ed09c65927a7ef28019.jpg)
![算法提高之动态规划:树形dp_第7张图片](http://img.e-com-net.com/image/info8/46dc319faca14336a26b54dce4b29a8e.jpg)
![算法提高之动态规划:树形dp_第8张图片](http://img.e-com-net.com/image/info8/059d02b2e5ed4c09b930f6caa9dab589.jpg)
![算法提高之动态规划:树形dp_第9张图片](http://img.e-com-net.com/image/info8/898dde86f85948cb98fd4c1acd91ab65.jpg)
![算法提高之动态规划:树形dp_第10张图片](http://img.e-com-net.com/image/info8/4e4fcade4d2848a3bd151c9777fb3ccf.jpg)
#include
#include
#include
using namespace std;
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], p1[N], up[N];
bool is_leaf[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;
p1[u] = j;
}
else if (d > d2[u]) d2[u] = d;
}
if (d1[u] == -INF)
{
d1[u] = d2[u] = 0;
is_leaf[u] = true;
}
return d1[u];
}
void dfs_u(int u, int father)
{
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 = d1[1];
for (int i = 2; i <= n; i ++ )
if (is_leaf[i]) res = min(res, up[i]);
else res = min(res, max(d1[i], up[i]));
printf("%d\n", res);
return 0;
}
2、树的中心
![算法提高之动态规划:树形dp_第11张图片](http://img.e-com-net.com/image/info8/68f27a5ba54740e6951e5bf9172273a9.jpg)
![算法提高之动态规划:树形dp_第12张图片](http://img.e-com-net.com/image/info8/bc3b505e9c964e3d95139b7dccf27fd0.jpg)
![算法提高之动态规划:树形dp_第13张图片](http://img.e-com-net.com/image/info8/38614bd588974b0c97a96defa28ecdea.jpg)
![算法提高之动态规划:树形dp_第14张图片](http://img.e-com-net.com/image/info8/f9c43fc8d37644ef8e5dda5d5a885f57.jpg)
#include
#include
#include
using namespace std;
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], p1[N], up[N];
bool is_leaf[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;
p1[u] = j;
}
else if (d > d2[u]) d2[u] = d;
}
if (d1[u] == -INF)
{
d1[u] = d2[u] = 0;
is_leaf[u] = true;
}
return d1[u];
}
void dfs_u(int u, int father)
{
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 = d1[1];
for (int i = 2; i <= n; i ++ )
if (is_leaf[i]) res = min(res, up[i]);
else res = min(res, max(d1[i], up[i]));
printf("%d\n", res);
return 0;
}
3、数字转换(树的最长路径)
![算法提高之动态规划:树形dp_第15张图片](http://img.e-com-net.com/image/info8/c847e1664a654ab1af777d0f2b09309f.jpg)
![算法提高之动态规划:树形dp_第16张图片](http://img.e-com-net.com/image/info8/d57f57ff85d1459d90f658854021299b.jpg)
![算法提高之动态规划:树形dp_第17张图片](http://img.e-com-net.com/image/info8/b48f6fe056584fed85ee99f07dffc8f9.jpg)
![算法提高之动态规划:树形dp_第18张图片](http://img.e-com-net.com/image/info8/c05eef03ef7c4a92b111495bb6e90f86.jpg)
![算法提高之动态规划:树形dp_第19张图片](http://img.e-com-net.com/image/info8/d58f6708797b4413a44e34d03a04b3ec.jpg)
![算法提高之动态规划:树形dp_第20张图片](http://img.e-com-net.com/image/info8/31709fcaa783452ca504b60f5b403a13.jpg)
![算法提高之动态规划:树形dp_第21张图片](http://img.e-com-net.com/image/info8/5f35065614dd47bebd1a60ce54eaab10.jpg)
#include
#include
#include
using namespace std;
const int N = 50010, M = N;
int n;
int h[N], e[M], w[M], ne[M], idx;
int sum[N];
bool 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!=-1; 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;
memset(h, -1, sizeof h);
for (int i = 1; i <= n; i ++ )
for (int j = 2; j <= n / i; j ++ )
sum[i * j] += i;
for (int i = 2; i <= n; i ++ )
if (sum[i] < 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;
}
4、二叉苹果树(完全二叉树 有依赖的背包问题)
![算法提高之动态规划:树形dp_第22张图片](http://img.e-com-net.com/image/info8/e3d5c9b664bd450d944e302cf8bc6b47.jpg)
![算法提高之动态规划:树形dp_第23张图片](http://img.e-com-net.com/image/info8/a8fd348058e54d548649b678e3f2abb6.jpg)
![算法提高之动态规划:树形dp_第24张图片](http://img.e-com-net.com/image/info8/a2ddf0dd088648729642c424a11997c1.jpg)
![算法提高之动态规划:树形dp_第25张图片](http://img.e-com-net.com/image/info8/95b14430c5bd49c7b66688606e599dde.jpg)
![算法提高之动态规划:树形dp_第26张图片](http://img.e-com-net.com/image/info8/bc075d1cc0c24ee9a694665beb287ece.jpg)
![算法提高之动态规划:树形dp_第27张图片](http://img.e-com-net.com/image/info8/9412951152744e12bb4e4d3ff5359670.jpg)
#include
#include
#include
using namespace std;
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 + 1 <= 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;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c), add(b, a, c);
}
dfs(1, -1);
printf("%d\n", f[1][m]);
return 0;
}
5、战略游戏
![算法提高之动态规划:树形dp_第28张图片](http://img.e-com-net.com/image/info8/13200a5e96bc4d8e99dda1266fe86eca.jpg)
![算法提高之动态规划:树形dp_第29张图片](http://img.e-com-net.com/image/info8/20664d877a5f483382b47da87e1bfb02.jpg)
![算法提高之动态规划:树形dp_第30张图片](http://img.e-com-net.com/image/info8/5262c2fda09e493b979b51b8dd35358e.jpg)
![算法提高之动态规划:树形dp_第31张图片](http://img.e-com-net.com/image/info8/1d780407a9844a94b5bd59ec06bd29cb.jpg)
![算法提高之动态规划:树形dp_第32张图片](http://img.e-com-net.com/image/info8/e10c09e5c4a4417583373da77e96a833.jpg)
![算法提高之动态规划:树形dp_第33张图片](http://img.e-com-net.com/image/info8/109bf16672ab49c7898ee1c3baddab57.jpg)
![算法提高之动态规划:树形dp_第34张图片](http://img.e-com-net.com/image/info8/deb17058b6964f80a52aba0623e1e62b.jpg)
#include
#include
#include
using namespace std;
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 (cin >> n)
{
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;
cin >> 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;
}
6、皇宫看守
![算法提高之动态规划:树形dp_第35张图片](http://img.e-com-net.com/image/info8/0f0cfae43ba4420eb953ad8eeaedb8fb.jpg)
![算法提高之动态规划:树形dp_第36张图片](http://img.e-com-net.com/image/info8/e2e09891257340058d0ca9e5352ca526.jpg)
![算法提高之动态规划:树形dp_第37张图片](http://img.e-com-net.com/image/info8/d700ed3486a64736ac457a8249ba627e.jpg)
![算法提高之动态规划:树形dp_第38张图片](http://img.e-com-net.com/image/info8/80073735545549b59f561015a84e8ea9.jpg)
![算法提高之动态规划:树形dp_第39张图片](http://img.e-com-net.com/image/info8/eb4c22faae294b198a962d475982d40b.jpg)
![算法提高之动态规划:树形dp_第40张图片](http://img.e-com-net.com/image/info8/ab761e71a5e84e2e81dc4edc1da5677d.jpg)
![算法提高之动态规划:树形dp_第41张图片](http://img.e-com-net.com/image/info8/e6a1b5d959bc429dac24c7101307852d.jpg)
#include
#include
#include
using namespace std;
const int N = 1510;
int n;
int h[N], w[N], e[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];
int sum = 0;
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]);
sum += min(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], sum - min(f[j][1], f[j][2]) + 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;
}
解释sum的用处
![在这里插入图片描述](http://img.e-com-net.com/image/info8/b10f626ab4524d16a3b33d97b489dab3.jpg)
![算法提高之动态规划:树形dp_第42张图片](http://img.e-com-net.com/image/info8/532ceeea31eb490a82597da491afae17.jpg)
#include
#include
#include
using namespace std;
const int N = 1510;
int n;
int h[N], w[N], e[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[u][0] - min(f[j][1], f[j][2]) + 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;
}