当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

Flask框架——flask-caching缓存

Flask框架——Session与Cookie

从开源的视角,解析SAP经典ERP “三十年不用变”的架构设计

HCIP(12)

Microwave technology homework course design - Discrete capacitance and inductance + microstrip single stub + microstrip double stub

Flask framework - session and cookies

Basic experiment of microwave technology - Filter Design

湖仓一体电商项目(二):项目使用技术及版本和基础环境准备

代码的表示学习:CodeBERT及其他相关模型介绍

Flame framework - Flame WTF form: file upload, verification code
随机推荐
Using px2rem does not take effect
Gan, why '𠮷 𠮷'.Length== 3 ??
Understand the life cycle and route jump of small programs
Use three.js to realize the cool cyberpunk style 3D digital earth large screen
disabled和readonly 以及焦点问题
redis 哨兵,高可用的执行者
JDBC的APi补充
从开源的视角,解析SAP经典ERP “三十年不用变”的架构设计
微波技术大作业课设-分立电容电感+微带单枝短截线+微带双枝短截线
2021 CEC written examination summary
Several common network diagnostic commands
UE4 collision
2021 written examination summary of niuke.com 01
js 哈希表 01
美国机场围棋风格可视化专题图:ArcGIS Pro版本
Learning Weekly - total issue 63 - an open source local code snippet management tool
代码的表示学习:CodeBERT及其他相关模型介绍
ONNX Runtime介绍
[Blue Bridge Cup training 100 questions] scratch Taiji diagram Blue Bridge Cup scratch competition special prediction programming question centralized training simulation exercise question No. 22
HCIP实验(01)