当前位置:网站首页>树形动态规划
树形动态规划
2022-07-25 09:56:00 【中学生信竞】
1582:周年纪念晚会
#include <bits/stdc++.h>
using namespace std;
const int N = 6009;
int n, p[N], h[N];
//f[u][0]:以u为根的子树,不含u的最大快乐度
//f[u][1]:以u为根的子树,包含u的最大快乐度
int f[N][2];
vector <int> e[N];
void dfs(int u) {
f[u][1] = p[u];
for (int i = 0; i < e[u].size(); i ++) {
int v = e[u][i];
dfs(v);
f[u][1] += f[v][0]; //如果u来,则v一定不来
f[u][0] += max(f[v][0], f[v][1]); //如果u不来,则v可来可不来
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i ++) scanf("%d", &p[i]);
int l, k;
while (scanf("%d%d", &l, &k) && l || k) {
e[k].push_back(l);
h[l] = 1;
}
int root;
for (int i = 1; i <= n; i ++) {
if (!h[i]) {
root = i;
break;
}
}
dfs(root);
printf("%d", max(f[root][0], f[root][1]));
return 0;
}
1578:战略游戏
#include <bits/stdc++.h>
using namespace std;
const int N = 1509;
int n, h[N];
//f[u][0]:以u为根的子树,u点放置时的最小士兵数
//f[u][1]:以u为根的子树,u点不放时的最小士兵数
int f[N][2];
vector <int> e[N];
void dfs(int u) {
f[u][1] = 1; //u结点放置士兵
for (int i = 0; i < e[u].size(); i ++) {
int v = e[u][i];
dfs(v);
//本题要求每条边都被观察到
//如果u结点放置士兵,则v结点可放可不放
//如果u结点不放士兵,则v结点必须放
f[u][1] += min(f[v][0], f[v][1]);
f[u][0] += f[v][1];
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i ++) {
int u, k, r;
scanf("%d%d", &u, &k);
for (int j = 1; j <= k; j ++) {
scanf("%d", &r);
e[u].push_back(r);
h[r] = 1;
}
}
int root;
for (int i = 0; i < n; i ++) {
if (!h[i]) {
root = i;
break;
}
}
dfs(root);
printf("%d", min(f[root][0], f[root][1]));
return 0;
}
1575:二叉苹果树
#include <bits/stdc++.h>
using namespace std;
const int N = 109;
int n, q;
int f[N][N]; //f[u][j]:以u为根的子树,保留j根树枝的苹果数量
vector <pair<int, int> > e[N]; //to, weight
void dfs(int u, int fa) {
for (int i = 0; i < e[u].size(); i ++) {
int v = e[u][i].first;
int w = e[u][i].second;
if (v == fa) continue;
dfs(v, u);
for (int j = q; j > 0; j --) {
//以u为根的子树保留j根
for (int k = 0; k < j; k ++) {
//以v为根的子树保留k根,另有一根连接u和v
f[u][j] = max(f[u][j], f[u][j - 1 - k] + f[v][k] + w);
}
}
}
}
int main() {
scanf("%d%d", &n, &q);
for (int i = 1; i < n; i ++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
e[u].push_back(make_pair(v, w));
e[v].push_back(make_pair(u, w));
}
dfs(1, 0); //根结点编号1,虚拟结点0
printf("%d", f[1][q]);
return 0;
}
边栏推荐
- 使用Three.js实现炫酷的赛博朋克风格3D数字地球大屏
- Configure FTP virtual user and access control
- 4.隔壁小孩都会的,各种shell符号{}[]等
- 7. Shell practical gadget cut, etc
- 8. SHELL file processing Three Musketeers sed
- The practice of asynchronous servlet in image service
- 10.expect免交互
- Storage, computing, distributed Virtualization (collection and sorting is suitable for Xiaobai)
- Keras深度学习实战(16)——自编码器详解
- Configuration of static routes (take Huawei ENSP as an example)
猜你喜欢
随机推荐
QT | mouse events and wheel events qmouseevent, qwheelevent
Gan, why '𠮷 𠮷'.Length== 3 ??
Implementation of recommendation system collaborative filtering in spark
2021 written examination summary of niuke.com 01
Research summary of voice self-monitoring pre training model CNN encoder
Introduction to onnx (open neural network exchange)
使用Three.js实现炫酷的赛博朋克风格3D数字地球大屏
3. Believe you can understand! Circular statements and functions of shell scripts, arrays, bubble sorting
ONNX(Open Neural Network Exchange)介绍
C3d model pytorch source code sentence by sentence analysis (II)
接口流量突增,如何做好性能调优?
2. Conditional statements of shell script
Keras深度学习实战(16)——自编码器详解
4、 Testfixture test fixture, or test firmware
[strategic mode] like Zhugeliang's brocade bag
UE4 window control (maximize minimize)
Electromagnetic field and electromagnetic wave experiment I familiar with the application of MATLAB software in the field of electromagnetic field
我为OpenHarmony 写代码,战“码”先锋第二期正式开启!
电磁场与电磁波实验一 熟悉Matlab软件在电磁场领域的应用
When installing mysql, string the service installation failure > mysql80 startup failure








