CF1092 --- Tree with Maximum Cost

题干

You are given a tree consisting exactly of \(n\) vertices. Tree is a connected undirected graph with \(n−1\) edges. Each vertex \(v\) of this tree has a value \(a_v\) assigned to it.

Let \(dist(x,y)\) be the distance between the vertices \(x\) and \(y\). The distance between the vertices is the number of edges on the simple path between them.

Let's define the cost of the tree as the following value: firstly, let's fix some vertex of the tree. Let it be \(v\). Then the cost of the tree is \(\sum\limits_{i=1}^{n}dist(i, v)a_i\)

Your task is to calculate the maximum possible cost of the tree if you can choose \(v\) arbitrarily.

\(\mathcal{Input}\)

The first line contains one integer \(n\), the number of vertices in the tree \((1\leq n\leq 2⋅10^5)\).

The second line of the input contains \(n\) integers \(a_1,a_2, \cdots ,a_n \; (1\leq a_i\leq 2⋅10^5)\), where \(a_i\) is the value of the vertex \(i\).

Each of the next \(n−1\) lines describes an edge of the tree. Edge \(i\) is denoted by two integers \(u_i\) and \(v_i\), the labels of vertices it connects \((1\leq u_i,v_i\leq n, u_i\not= v_i).\)

It is guaranteed that the given edges form a tree.

\(\mathcal{Output}\)

Print one integer — the maximum possible cost of the tree if you can choose any vertex as \(v\).

\(\mathcal{Example}\)

\(Case_1\)

\(Input\)

8

9 4 1 7 10 1 6 5

1 2

2 3

1 4

1 5

5 6

5 7

5 8

\(Output\)

121

\(Case_2\)

\(Input\)

1

1337

\(Output\)

0

\(\mathcal{Note}\)

Picture corresponding to the first example:



You can choose the vertex \(3\) as a root, then the answer will be \(2⋅9+1⋅4+0⋅1+3⋅7+3⋅10+4⋅1+4⋅6+4⋅5=18+4+0+21+30+4+24+20=121\).

In the second example tree consists only of one vertex so the answer is always \(0\).

\(\mathcal{Tag}\)

dfs and similar dp tree *1800

思路分析

 本题要求解的是,对于树中所有结点,以该节点为根的情况下计算费用,并求费用的最大值。注意在结点非常多的情况下,要考虑\(int\)溢出的问题,故开\(long\;long\)(坑死我了)

暴力想法

 暴力想法很简单,很类似CF1324 --- Maximum White Subtree,这里直接搬运那个题解里面的图片



 对于任意结点,分别计算不同路径的位置,根据树数据结构的特点,可以把贡献分为:

  • 上层父祖先节点贡献
  • 下层子孙结点贡献

 然后就是,如何把暴力\(dfs\),通过记忆化实现算法优化。

算法优化

 我们先求解下层子孙结点的贡献,通过题目我们知道,从结点\(v\)转移到结点\(u\),其中增量的就是以\(v\)为根结点,其子树中所有结点值的和。其中\(a_i\)代表结点\(i\)的值,我们设\(f_i\)代表以\(i\)结点为子树根结点,该子树所有结点的和。我们设\(hp_i\)代表以\(i\)结点的下层子孙贡献值,这样我们可以写出下层子孙结点贡献值的转移表达值:

\[f[u] = f[v] + a[u]
\]

\[hp[u] = hp[v] + f[v]
\]

 现在我们解决了下层得想办法解决更加困难的上层了,对于这类换根dp问题,常用的手段是用利用父节点的值进行状态转移。在这里我们设\(dp_i\)为结点\(i\)的最终费用。因此对于某一节点(非根结点),该节点的最终费用可以表示为:

\[dp[cur] = dp[fa] - hp[cur] - f[cur] + f[fa] - f[cur] + hp[cur] = dp[fa] + f[fa] - 2*f[cur]
\]

 上述表达式的意思为:父节点刨去结点\(cur\)的费用值(\(dp[fa] - hp[cur] - f[cur]\),加上增量(\(f[fa] - f[cur]\), note 这里的f[fa],代表着所有的结点和),再最后加上下层子孙结点贡献值(\(hp[cur]\)).

\[dp[v] = \left\{\begin{array}
1hp[v] & if\; v = root \\
dp[fa] + f[fa] - 2*f[v] & if\; v \not= root\\
\end{array}\right.
\]

&emps;因此我们最终的思路还是为:

  • 从下往上树形\(dp\),计算\(f_v\),\(hp_v\)
  • 从上往下换根\(dp\),计算\(dp_v\)

代码

#include<bits/stdc++.h>
using namespace std; using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
LL mx = 0x8000000000000000;
VL a, f, hp, dp;
VVL e; void dfs(int x, int fa = -1)
{
f[x] = a[x], hp[x] = 0;
for (auto to : e[x])
{
if (to == fa) continue;
dfs(to, x);
f[x] += f[to];
hp[x] += (hp[to] + f[to]);
}
} void rdfs(int x, int fa = -1)
{
dp[x] = hp[x];
mx = max(mx, dp[x]);
for (auto to : e[x])
{
if (to == fa) continue;
hp[to] = dp[x] + f[x] - 2*f[to];
f[to] = f[x];
rdfs(to, x);
}
} int main()
{
int n;
cin >> n;
a = f = hp = dp = VL(n);
e = VVL(n); for (auto &x : a) cin >> x;
for (int i = 0; i < n - 1; ++ i)
{
int x, y;
cin >> x >> y;
-- x, -- y;
e[x].push_back(y);
e[y].push_back(x);
}
dfs(0);
rdfs(0);
cout << mx << endl;
return 0;
}

 做了两次了,这个1092是自己手打的,还是有进步。WA了一次是没有考虑到整数溢出的情况以后得多加考虑。

以后对于每个根结点都要求的题,考虑优先换根dp

CF1092 --- Tree with Maximum Cost的更多相关文章

  1. Codeforces 1092F Tree with Maximum Cost(树形DP)

    题目链接:Tree with Maximum Cost 题意:给定一棵树,树上每个顶点都有属性值ai,树的边权为1,求$\sum\limits_{i = 1}^{n} dist(i, v) \cdot ...

  2. Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS换根 || 树形dp】

    传送门:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 sec ...

  3. Codeforces Round #527 (Div. 3) . F Tree with Maximum Cost

    题目链接 题意:给你一棵树,让你找一个顶点iii,使得这个点的∑dis(i,j)∗a[j]\sum dis(i,j)*a[j]∑dis(i,j)∗a[j]最大.dis(i,j)dis(i,j)dis( ...

  4. 2018.12.19 codeforces 1092F. Tree with Maximum Cost(换根dp)

    传送门 sbsbsb树形dpdpdp题. 题意简述:给出一棵边权为1的树,允许选任意一个点vvv为根,求∑i=1ndist(i,v)∗ai\sum_{i=1}^ndist(i,v)*a_i∑i=1n​ ...

  5. CF F - Tree with Maximum Cost (树形DP)给出你一颗带点权的树,dist(i, j)的值为节点i到j的距离乘上节点j的权值,让你任意找一个节点v,使得dist(v, i) (1 < i < n)的和最大。输出最大的值。

    题目意思: 给出你一颗带点权的树,dist(i, j)的值为节点i到j的距离乘上节点j的权值,让你任意找一个节点v,使得dist(v, i) (1 < i < n)的和最大.输出最大的值. ...

  6. Codeforces Round #527 F - Tree with Maximum Cost /// 树形DP

    题目大意: 给定一棵树 每个点都有点权 每条边的长度都为1 树上一点到另一点的距离为最短路经过的边的长度总和 树上一点到另一点的花费为距离乘另一点的点权 选定一点出发 使得其他点到该点的花费总和是最大 ...

  7. Codeforces 1092 F Tree with Maximum Cost (换根 + dfs)

    题意: 给你一棵无根树,每个节点有个权值$a_i$,指定一个点u,定义$\displaystyle value = \sum^v a_i*dist(u,v)$,求value的最大值 n,ai<= ...

  8. CF1092F Tree with Maximum Cost(dfs+dp)

    果然我已经菜到被\(div3\)的题虐哭了 qwq 首先看到这个题,一个比较显然的想法就是先从1号点开始\(dfs\)一遍,然后通过一些奇怪的方式,再\(dfs\)一遍得到其他点的贡献. 那么具体应该 ...

  9. 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...

随机推荐

  1. 【cs224w】Lecture 6 - 消息传递 及 节点分类

    目录 Node Classification Probabilistic Relational Classifier Iterative Classification Belief Propagati ...

  2. JSP+Struts2+JDBC+Mysql实现的校园宿舍管理系统

    项目简介 项目来源于:https://gitee.com/passenger134/javaweb-sushe 本系统基于JSP+Struts2+JDBC+Mysql的校园宿舍管理系统.该系统没有使用 ...

  3. 原来rollup这么简单之插件篇

    大家好,我是小雨小雨,致力于分享有趣的.实用的技术文章. 内容分为翻译和原创,如果有问题,欢迎随时评论或私信,希望和大家一起进步. 大家的支持是我创作的动力. 计划 rollup系列打算一章一章的放出 ...

  4. 写一个TODO App学习Flutter本地存储工具Moor

    写一个TODO App学习Flutter本地存储工具Moor Flutter的数据库存储, 官方文档: https://flutter.dev/docs/cookbook/persistence/sq ...

  5. 下载SVN项目代码

    1. 到SVN根目录右键选中SVN Checkout...

  6. OS-DOS/CMD/Windows/各类软件快捷键等使用总结

    一.快捷键 很多软件的快捷键使用相通,在不确定的情况下,先试试其他软件的快捷键的使用方法 Windows电脑快捷键 HP惠普笔记本 win+E 打开文件管器 win+D 显示桌面 win+L 锁计算机 ...

  7. python字符串列表元组序列操作

    Table of Contents generated with DocToc python系列-字符串.列表.元组的操作 序列的访问及运算符 序列通用操作 访问单个元素 切片访问一部分元素 序列的复 ...

  8. 004-流程控制-C语言笔记

    004-流程控制-C语言笔记 学习目标 1.[掌握]关系运算符和关系表达式 2.[掌握]逻辑运算符和逻辑表达式 3.[掌握]运算符的优先级和结合性 4.[掌握]if-else if-else结构的使用 ...

  9. webWMS开发过程记录(二)- WMS是什么

    (参考:WMS-百度百科) 简介 WMS是仓库管理系统(Warehouse Management System)的缩写,是一款标准化.智能化过程导向管理的仓库管理软件仓库管理系统,是通过出入库业务.仓 ...

  10. 《带你装B,带你飞》pytest修仙之路5 - yield操作

    1. 简介 上一篇中,我们刚刚实现了在每个用例之前执行初始化操作,那么用例执行完之后如需要清除数据(或还原)操作,可以使用 yield 来实现.fixture通过scope参数控制setup级别,既然 ...