题目

题目描述
You are given a tree consisting exactly of nn vertices. Tree is a connected undirected graph with n-1n−1 edges. Each vertex vv of this tree has a value a_va
v

assigned to it.

Let dist(x, y)dist(x,y) be the distance between the vertices xx and yy . 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 vv . Then the cost of the tree is \sum\limits_{i = 1}^{n} dist(i, v) \cdot a_i
i=1

n

dist(i,v)⋅a
i

.

Your task is to calculate the maximum possible cost of the tree if you can choose vv arbitrarily.

输入格式
The first line contains one integer nn , the number of vertices in the tree ( 1 \le n \le 2 \cdot 10^51≤n≤2⋅10
5
).

The second line of the input contains nn integers a_1, a_2, \dots, a_na
1

,a
2

,…,a
n

( 1 \le a_i \le 2 \cdot 10^51≤a
i

≤2⋅10
5
), where a_ia
i

is the value of the vertex ii .

Each of the next n - 1n−1 lines describes an edge of the tree. Edge ii is denoted by two integers u_iu
i

and v_iv
i

, the labels of vertices it connects ( 1 \le u_i, v_i \le n1≤u
i

,v
i

≤n , u_i \ne v_iu
i



=v
i

).

It is guaranteed that the given edges form a tree.

输出格式
Print one integer — the maximum possible cost of the tree if you can choose any vertex as vv .

题意翻译
有一n个节点的树,每个节点有一点权ai。

定义dist(x,y)为x到y的边数。

选取一点v,使\sum_{i=1}^{n}dist(i,v)*ai\qquad∑
i=1
n

dist(i,v)∗ai最大

第一行输入n;

第二行输入n个数ai;

接下来n-1行,每行两个数x,y表示x到y有一条边。

输出最大值。

输入输出样例
输入 #1复制
8
9 4 1 7 10 1 6 5
1 2
2 3
1 4
1 5
5 6
5 7
5 8
输出 #1复制
121
输入 #2复制
1
1337
输出 #2复制
0
说明/提示
Picture corresponding to the first example:

You can choose the vertex 33 as a root, then the answer will be 2 \cdot 9 + 1 \cdot 4 + 0 \cdot 1 + 3 \cdot 7 + 3 \cdot 10 + 4 \cdot 1 + 4 \cdot 6 + 4 \cdot 5 = 18 + 4 + 0 + 21 + 30 + 4 + 24 + 20 = 1212⋅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 00 .

思路

1.第一次扫描时,任选一个点为根,再“有根树”上执行一次树形DP,也就是回溯时发生的 自底向上的状态转移

2.第二次扫描时,从刚才任选的根出发,对整棵树执行一次深度优先遍历,在,每次递归钱进行自顶向下的推导,计算出换根后的新解

套路比较明显

来看这道题 要求我们选择一个点 使得其他点的深度乘权值之和最大

我们先以一号点为根然后进行预处理 求出来子树的权值和 以及1号节点为根的DP值

考虑第二次扫描

当我们枚举到u-v这条边时 图被分成两部分 画图很显然可得 u节点所在的部分深度都+1 v节点所在深度都-1

所以DP方程呼之欲出:

f[v]=f[u]+sum-size[v]-size[v]

O(n)DFS即可

代码

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e6+77;
int n,head[N],cnt;
ll siz[N],sum,f[N],ans=0,a[N],dep[N];
struct edge
{int nxt,to;
}e[N<<1];
void add(int x,int y){e[++cnt].nxt=head[x];e[cnt].to=y;head[x]=cnt;
}
void dfs1(int x,int fa){siz[x]=a[x];for(int i=head[x]; i; i=e[i].nxt){int v=e[i].to;if(v==fa) continue;dep[v]=dep[x]+1;f[1]=f[1]+(ll)(a[v]*dep[v]);dfs1(v,x);siz[x]+=siz[v];}
}
void dfs2(int x,int fa){for(int i=head[x]; i; i=e[i].nxt){int y=e[i].to;if(y==fa) continue;f[y]=f[x]+sum-siz[y]-siz[y];dfs2(y,x);}
}
int main()
{n=read();for(int i=1; i<=n; i++){a[i]=read();sum+=a[i];}for(int i=1; i<n; i++){int u,v;u=read(),v=read(); add(u,v);add(v,u);}dfs1(1,0);dfs2(1,0);for(int i=1; i<=n; i++){ans=max(f[i],ans);}printf("%lld",ans);
}

【CF1092F】 Tree with Maximum Cost相关推荐

  1. 【CF791D】tree 柠檬树

    [题目描述] Herobrine能掌控所有,除了他内心的那棵柠檬树. 他每看到一件让自己心生羡慕的事,他内心的柠檬树上就会多长出一只多汁美味的柠檬. 现在,Herobrine有一棵含有n只柠檬的柠檬树 ...

  2. 【windows】【linux】tree命令

    前言 windows和linux都有tree命令,主要功能是创建文件列表,将所有文件以树的形式列出来 打印目录树 window中使用tree命令 来个tree命令 cmd> tree C:\ 命 ...

  3. 【EasyUI】Tree中自定义在节点前是否显示checkbox

    参考:https://blog.csdn.net/qq_40529542/article/details/90729145 在tree中利用checkbox显示复选框时,发现每个节点前面都有,如图所示 ...

  4. 【LCT】Tree II(luogu 1501)

    Tree II luogu 1501 题目大意 给出一棵树,让你进行若干操作,操作如下: 1.把两个点路径上的所有点权值加k 2.把两个点路径上的所有点权值乘k 3.把一条边断开,连上另一条边 4.查 ...

  5. div获取第一个子节点jquery_【antd】Tree组件子节点不完全勾选获取父节点的值

    注意⚠️:这篇文章适用于后台返回的树结构比较多的情况下,如果数据比较少的情况下,可以参考我的上一篇文章,操作起来比较简单 我们在实际操作tree组件和后台交互的时候一般都是需要将父节点传过去,如图:点 ...

  6. 【DP】LeetCode 53. Maximum Subarray

    LeetCode 53. Maximum Subarray Solution1:我的答案 动态规划 class Solution { public:int maxSubArray(vector< ...

  7. 【动态规划】LeetCode 53. Maximum Subarray

    LeetCode 53. Maximum Subarray 原题描述(求子序列最大和/最大子串):Find the contiguous subarray within an array (conta ...

  8. 【Leetcode】1774. Closest Dessert Cost

    题目地址: https://leetcode.com/problems/closest-dessert-cost/description/ 给定两个数组AAA和BBB,分别长n,mn,mn,m.AAA ...

  9. 【BZOJ2212】【POI2011】Tree Rotations(线段树合并)

    Description click me Solution 对于每个节点有一棵权值线段树,向上递归时合并同时计算逆序对即可. Source /***************************** ...

最新文章

  1. CNN+Transformer=SOTA!CNN丢掉的全局信息,Transformer来补
  2. 商品规格可选怎么设计_商品模块数据库表解析(一)
  3. 如何设置Flutter
  4. 【机器学习基础】太棒了!这里有385篇自然语言处理和机器学习领域的综述总结...
  5. .NET 基金会项目介绍 - ReactiveUI
  6. SpringBoot 扫描包
  7. linux系统用户管理
  8. 一点一点看JDK源码(二)java.util.List
  9. 用私有构造器或枚举类型强化Singleton属性(3)
  10. 关于JSF Converter转换器的知识点
  11. 许愿墙|爱墙 js代码
  12. Java内存模型以及happens-before规则
  13. 杆刚度校核c语言程序,c语言 求解单元刚度矩阵
  14. Session的活化与钝化
  15. 去除VScode中的蓝色波浪线
  16. 百分比计算机公式,百分比的计算公式怎么算的(免费教你计算百分比)
  17. html多行合并,Js表格多行合并实现,可对多个列进行处理
  18. html+css设置背景图移动以及人物行走的动画效果
  19. 怎么教你如何查看电脑的蓝牙版本【解决方案】
  20. 【LDPC-11】基于QC-LDPC的CDR系统LDPC编码实现与matlab仿真验证

热门文章

  1. 【Python小案例教程1】Python开发简单记事本
  2. 上墙抽奖php代码,微信帐号开发:独立PHP微信上墙|微信墙|微信抽奖完美版(PHP源码)...
  3. 精品网址导航主题整站源码 wordpress模板 自适应手机端
  4. Java获取项目当前请求的全部URL,Java获取Referer,Java获取完整链接地址URL
  5. ubuntu 下安装fetion
  6. LeetCode——1824. 最少侧跳次数(Minimum Sideway Jumps)[中等]——分析及代码(Java)
  7. C 三个学生四门成绩,求成绩总和与平均值
  8. 当下这个时代,一个没学历智力情商也低的人,他是如何破局的?深度思考后的回答!...
  9. 2018东北四省赛 Spin A Web 曼哈顿距离最小生成树
  10. 王国维《人间词话》:“古今之成大事业、大学问者,必经过三种之境界”