当前位置:网站首页>CF533B(树形dp+转移技巧)
CF533B(树形dp+转移技巧)
2022-08-08 17:41:00 【野指针*】
题意:
有一颗n个节点并且以1为根的树,每一个节点有一个权值,你可以选择一些节点组成工作组,工作中中的每一个节点他的子树上的节点在工作组的数目为偶数(不包括自己),求工作组的最大权值为多少(工作组的权值等于工作组中每个节点的权值之和)?
思路:
显然是用树形dp解决,假设f(i, 1/0)为以i为根的子树的最大权值.此时考虑转移,
初始化f(i, 0) = 0, f(i, 1) = -INF,表示当前这棵子树还没选i这个点.对于转移,我自己是这么想的,枚举每个点选奇数还是选偶数,但是会爆时间.我们可以再套个dp进行转移.假设u -> v
f(u, 0) = max(f(u, 0) + f(v, 0), f(u, 1) + f(v, 1)) 偶 + 偶 = 奇 + 奇 = 偶
f(u, 1) = max(f(u, 1) + f(v, 0), f(u, 0) + f(v, 1)) 偶 + 奇 = 奇 + 偶 = 奇
代码:
#include <bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define ll long long
#define double long double
#define ull unsigned long long
#define PII pair<int, int>
#define PDI pair<double, int>
#define PDD pair<double, double>
#define debug(a) cout << #a << " = " << a << endl
#define point(n) cout << fixed << setprecision(n)
#define all(x) (x).begin(), (x).end()
#define mem(x, y) memset((x), (y), sizeof(x))
#define lbt(x) (x & (-x))
#define SZ(x) ((x).size())
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
namespace nqio{const unsigned R = 4e5, W = 4e5; char *a, *b, i[R], o[W], *c = o, *d = o + W, h[40], *p = h, y; bool s; struct q{void r(char &x){x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++;} void f(){fwrite(o, 1, c - o, stdout); c = o;} ~q(){f();}void w(char x){*c = x;if (++c == d) f();} q &operator >>(char &x){do r(x);while (x <= 32); return *this;} q &operator >>(char *x){do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this;} template<typename t> q&operator>>(t &x){for (r(y),s = 0; !isdigit(y); r(y)) s |= y == 45;if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this;} q &operator <<(char x){w(x);return *this;}q &operator<< (char *x){while (*x) w(*x++); return *this;}q &operator <<(const char *x){while (*x) w(*x++); return *this;}template<typename t> q &operator<< (t x) {if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p);return *this;}}qio; }using nqio::qio;
using namespace std;
const int N = 2e5 + 10;
int n, a[N], f[N][2], root;
vector<int> son[N];
//f[i][1/0]表示以i为根的子树中职员有奇数个/偶数个的最大权值
void dfs(int x, int fa) {
f[x][1] = -INF, f[x][0] = 0;
for (int y : son[x]) {
if (y == fa) continue;
dfs(y, x);
int x0 = f[x][0], x1 = f[x][1];
f[x][0] = max(f[y][1] + x1, f[y][0] + x0);
f[x][1] = max(f[y][0] + x1, f[y][1] + x0);
}
f[x][1] = max(f[x][1], f[x][0] + a[x]);
}
void solve() {
qio >> n;
for (int i = 1, x; i <= n; ++i) {
qio >> x >> a[i];
if (x == -1)
root = i;
else {
son[x].emplace_back(i);
son[i].emplace_back(x);
}
}
dfs(root, -1);
qio << max(f[root][1], f[root][2]) << "\n";
}
signed main() {
int T = 1;
while (T--) solve();
}
边栏推荐
- 记录贴:pytorch学习Part4
- spark学习笔记(八)——sparkSQL概述-定义/特点/DataFrame/DataSet
- 【云图说】第252期 初识云速建站服务
- Getting started with glide4
- arxiv国内镜像——快速下载
- 企业“数字化转型”成功的2个必备条件!
- grpc服务发现&负载均衡
- orbslam2实验记录-----稠密建图
- Are Huishang Futures official and reliable?Is it safe to open an account in Huishang Futures?
- Reprinted, the fragment speaks very well, the big guy
猜你喜欢
Cholesterol-PEG-DBCO,CLS-PEG-DBCO,胆固醇-聚乙二醇-二苯基环辛炔一种环炔烃
基于simulink的风力机房温度控制系统仿真
spark学习笔记(八)——sparkSQL概述-定义/特点/DataFrame/DataSet
以数治企,韧性成长,2022 年中国 CIO 数字峰会成功举行
win10如何设置定时联网断网辅助自律
slam测评工具evo的安装与使用
从2022投影行业最新报告,读懂2022年家用智能投影仪该怎么选!
4. S32K14X study notes: S32 Design Studio new and imported projects
迁移学习(Transfer Learning)的背景、历史
How to set timed network disconnection to assist self-discipline in win10
随机推荐
L2-011 玩转二叉树 (25 分) (二叉树)
1.初识MySQL数据库
Cuda Anaconda tensorflow 版本对应
【AI玩家养成记 • 第3期】AI开发者必备!史上最适合新手的昇腾AI环境搭建教程!!
dp,dpi,px知识补充
Tensorflow教程(二)——基本概念与搭建流程
win10如何设置定时联网断网辅助自律
21天学习第五天--数组
使用train_test_split划分训练数据集、测试数据集
开源一夏 | 疫情期间闲来无事,我自制了一个按钮展示框特效来展示我的博客
【教程2】疯壳·ARM功能手机-测试程序介绍
史上最强IDEA工具使用教程,你想要的全都有!
L2-010 排座位 (25 分) (DFS)
glide4入门
mysql中模糊查询的四种用法介绍
Go源码之原子操作(atomic)
How to set timed network disconnection to assist self-discipline in win10
LeetCode_Binary Tree_Medium_515. Find the maximum value in each tree row
使用电脑通过VNC Viewer远程连接树莓派4B
IP分配——DHCP(讲解+配置)