博客
关于我
【nowcoder 217042】CCA的子树
阅读量:331 次
发布时间:2019-03-04

本文共 1535 字,大约阅读时间需要 5 分钟。

CCA的子树

题目链接:

到牛客看:

题目大意

有一个带点权的树,根节点是 1 1 1

要你选出两个点,它们不会一个是另一个的祖先,然后你要让它们子树点权和的和最大。
如果不能选出两个点,就输出 Error

思路

我们可以想到这题应该是树形 DP。

考虑先 DP 出某个点的子树的点权和 q i q_i qi,然后可以想到用这样一种方法。

我们在 DP 出某个点的子树中的每个点 x x x q x q_x qx 值的最大值 b i g _ s o n i big\_son_i big_soni
那我们考虑枚举你选的两个点的最近公共祖先,然后就把它子树的 b i g _ s o n big\_son big_son 值最大的两个相加,得到的就是这个点为最近公共祖先时答案能有的最大值。

那你就把所有的取一个最大值,就可以了。

代码

#include
#include
#define ll long longusing namespace std;struct node { int to, nxt;}e[400001];int n, a[200001], x, y;int KK, le[200001], maxn_dep;ll ans, big_son[200001], sum[200001];void add(int x, int y) { e[++KK] = (node){ y, le[x]}; le[x] = KK; }void dfs(int now, int father, int dep) { maxn_dep = max(maxn_dep, dep); big_son[now] = -0x3f3f3f3f3f3f3f3f; sum[now] = a[now]; ll maxn = -0x3f3f3f3f3f3f3f3f, maxn2 = -0x3f3f3f3f3f3f3f3f; for (int i = le[now]; i; i = e[i].nxt) if (e[i].to != father) { dfs(e[i].to, now, dep + 1); big_son[now] = max(big_son[now], big_son[e[i].to]); sum[now] += sum[e[i].to]; if (big_son[e[i].to] > maxn) { maxn2 = maxn; maxn = big_son[e[i].to]; } else if (big_son[e[i].to] > maxn2) maxn2 = big_son[e[i].to]; } big_son[now] = max(big_son[now], sum[now]); ans = max(ans, maxn + maxn2);}int main() { ans = -0x3f3f3f3f3f3f3f3f; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i < n; i++) { scanf("%d %d", &x, &y); add(x, y); add(y, x); } dfs(1, 0, 1); if (maxn_dep == n) printf("Error"); else printf("%lld", ans); return 0;}

转载地址:http://frvh.baihongyu.com/

你可能感兴趣的文章
MySQL Error Handling in Stored Procedures---转载
查看>>
MVC 区域功能
查看>>
MySQL FEDERATED 提示
查看>>
mysql generic安装_MySQL 5.6 Generic Binary安装与配置_MySQL
查看>>
Mysql group by
查看>>
MySQL I 有福啦,窗口函数大大提高了取数的效率!
查看>>
mysql id自动增长 初始值 Mysql重置auto_increment初始值
查看>>
MySQL in 太多过慢的 3 种解决方案
查看>>
MySQL InnoDB 三大文件日志,看完秒懂
查看>>
Mysql InnoDB 数据更新导致锁表
查看>>
Mysql Innodb 锁机制
查看>>
MySQL InnoDB中意向锁的作用及原理探
查看>>
MySQL InnoDB事务隔离级别与锁机制深入解析
查看>>
Mysql InnoDB存储引擎 —— 数据页
查看>>
Mysql InnoDB存储引擎中的checkpoint技术
查看>>
Mysql InnoDB存储引擎中缓冲池Buffer Pool、Redo Log、Bin Log、Undo Log、Channge Buffer
查看>>
MySQL InnoDB引擎的锁机制详解
查看>>
Mysql INNODB引擎行锁的3种算法 Record Lock Next-Key Lock Grap Lock
查看>>
mysql InnoDB数据存储引擎 的B+树索引原理
查看>>
mysql innodb通过使用mvcc来实现可重复读
查看>>