当前位置:网站首页>Tree dynamic programming
Tree dynamic programming
2022-07-25 11:02:00 【Middle school student Xinjing】
1582: Anniversary party
#include <bits/stdc++.h>
using namespace std;
const int N = 6009;
int n, p[N], h[N];
//f[u][0]: With u Root subtree , Not included u Maximum happiness
//f[u][1]: With u Root subtree , contain u Maximum happiness
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]; // If u Come on , be v Definitely not
f[u][0] += max(f[v][0], f[v][1]); // If u Not coming , be v Come or not
}
}
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: Strategy game
#include <bits/stdc++.h>
using namespace std;
const int N = 1509;
int n, h[N];
//f[u][0]: With u Root subtree ,u The minimum number of soldiers when the point is placed
//f[u][1]: With u Root subtree ,u The minimum number of soldiers when clicking
int f[N][2];
vector <int> e[N];
void dfs(int u) {
f[u][1] = 1; //u Place soldiers at the node
for (int i = 0; i < e[u].size(); i ++) {
int v = e[u][i];
dfs(v);
// This question requires that each side be observed
// If u Place soldiers at the node , be v Nodes can be put or not
// If u Don't let the soldiers go , be v Nodes must be placed
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: A binary apple tree
#include <bits/stdc++.h>
using namespace std;
const int N = 109;
int n, q;
int f[N][N]; //f[u][j]: With u Root subtree , Retain j Number of apples per branch
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 --) {
// With u Reserved for the subtree of the root j root
for (int k = 0; k < j; k ++) {
// With v Reserved for the subtree of the root k root , There is another connection u and 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); // Root node number 1, Virtual node 0
printf("%d", f[1][q]);
return 0;
}
边栏推荐
- 2021 written examination summary of niuke.com 01
- I, AI doctoral student, online crowdfunding research topic
- Idea overall font size modification
- js 双向链表 02
- JS hash table 01
- Digital twin everything can be seen | connecting the real world and digital space
- 从开源的视角,解析SAP经典ERP “三十年不用变”的架构设计
- 电磁场与电磁波实验一 熟悉Matlab软件在电磁场领域的应用
- 【蓝桥杯集训100题】scratch太极图 蓝桥杯scratch比赛专项预测编程题 集训模拟练习题第22题
- 【信息系统项目管理师】思维导图系列精华汇总
猜你喜欢

HCIP实验(02)

性能测试中TPS的计算【杭州多测师】【杭州多测师_王sir】

ONNX(Open Neural Network Exchange)介绍

HCIP(11)

异步Servlet在转转图片服务的实践

Flask framework - flask WTF form: data validation, CSRF protection

JS hash table 02

2021 written examination summary of niuke.com 01

DHCP configuration (take Huawei ENSP as an example)

3. Like you, DNS domain name resolution service!!!
随机推荐
【云享新鲜】社区周刊·Vol.72- 2022华为开发者大赛中国区首场开幕式启动;华为云KooMessage火热公测中…
UE4 collision
Nb-iot control LCD (date setting and reading)
信号完整性(SI)电源完整性(PI)学习笔记(三十四)100条估计信号完整性效应的经验法则
6. PXE combines kickstart principle and configuration to realize unattended automatic installation
What is the meaning of ordinary people's life?
Idea overall font size modification
Microwave technology homework course design - Discrete capacitance and inductance + microstrip single stub + microstrip double stub
AI技术栈太庞大!吴恩达给出职业生涯规划:终生学习
C3d model pytorch source code sentence by sentence analysis (III)
Gan, why '𠮷 𠮷'.Length== 3 ??
Redis usage scenario
ONNX(Open Neural Network Exchange)介绍
Introduction to onnx (open neural network exchange)
UE4 framework introduction
AI technology stack is too huge! Wu Enda gives career planning: lifelong learning
[cloud enjoys freshness] community weekly · Vol 72 - the first opening ceremony of the 2022 Huawei developer competition in China was launched; Huawei cloud koomessage is in hot public beta
DICOM medical image viewing and browsing function based on cornerstone.js
Qt | 鼠标事件和滚轮事件 QMouseEvent、QWheelEvent
ESP32C3基于Arduino框架下的 ESP32 RainMaker开发示例教程