Tree with Maximum Cost---CF1092F 树上DP

简介: 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 av assigned to it.Let dist(x,y) be the distance between the vertices x and y.
                F. Tree with Maximum Cost
                time limit per test2 seconds
                memory limit per test256 megabytes
                inputstandard input
                outputstandard output


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 av 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 ∑ i = 1 n \sum_{i=1}^n∑

i=1

n


dist(i,v)⋅ai


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


Input


The first line contains one integer n, the number of vertices in the tree (1≤n≤2⋅105).


The second line of the input contains n integers a1,a2,…,an (1≤ai≤2⋅105), where ai 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 ui and vi, the labels of vertices it connects (1≤ui,vi≤n, ui≠vi).


It is guaranteed that the given edges form a tree.


Output


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


Examples


input 1


8
9 4 1 7 10 1 6 5
1 2
2 3
1 4
1 5
5 6
5 7
5 8


output 1


121


input 2


1
1337


output 2


0

Note

Picture corresponding to the first example:

微信图片_20220531180525.png

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.


题目大意:


给出一个具有n个点的无向图,给出 n-1条边,然后求出如果以一个点为树的根,那么这棵树的Cost是多少,Cost定义为 ∑ i = 1 n \sum_{i=1}^n∑

i=1

n


dist(i,v)⋅ai,dist(a,b)的含义就是两个点之间的距离

另外,数据保证该图联通


同学博客奉上


在以 1 为根节点的时候,如果这个根节点转移到与其相连的子节点,那么来说,和转移之前的节点相连的子树的节点的距离就要减小 1 ,假设a[t] 表示以 t 为根的子树的权值,那么来说,以之前的 1 为根的子树的权值就是a[1] , 如果是相连的子节点那么距离就要减少 a[i] , 然后对于那些非子节点的 Cost 就要增加 1,那么来说,增加的就是a[1] - a[i],所以说,总的变化就是

a[1] - a[i] - a[i]

则当前节点的Cost:

Cost[子节点] = Cost[父节点] + a[1] - a[i] - a[i]


深度优先搜索的时候遍历一下就行了,注意遍历的时候父节点子节点的关系问题


Code:


#include <bits/stdc++.h>
#include <cmath>
using namespace std;
typedef long long ll;
typedef int itn;
const int maxn = 2e5 + 7;
/// 链式前向星向存图
struct node {
  int pt;///当前节点
  int net;///下一个节点的
}p[maxn << 1];
int cnt = 0;
ll head[maxn];
ll pre[maxn];///预处理的权值
ll n;///总共有N个点
ll ans;///记录答案
ll a[maxn];
void _Add(int u, int v) {
  p[cnt].pt = v;///下一个节点;
  p[cnt].net = head[u];
  ///cnt++;
  head[u] = cnt++;
}
/// 初始化
void _Init() {
  for (int i = 0; i <= n; i++) {
    head[i] = -1;///初始化为 -1
    pre[i] = 0;
  }
}
void DFS(int x, int fa) {
  /// 当前节点和父节点
  for (int i = head[x]; ~i; i = p[i].net) {
    int pt = p[i].pt;///找到对应的节点
    if (pt == fa) continue;///所连向的点正好是父亲节点
    DFS(pt, x);///这时候,x就成了父亲节点,pt就是子节点
    /// wawawa此时的父节点是x
    a[x] += a[pt];///父节点的权值要加上这个子节点的权值
    /// wawawa应该是+=
    pre[x] += (pre[pt] + a[pt]);/// 父亲节点子树的成本就是其子节点树的成本加上这个点的权
  }
}
void dfs(int x, int fa) {
  if (x != 1) {
    ///这个点不是根节点的时候
    pre[x] = pre[fa] + a[1] - 2 * a[x];///逻辑关系
  }
  for (int i = head[x]; ~i; i = p[i].net) {
    int pt = p[i].pt;///获得当前指向的节
    if (pt == fa) continue;
    dfs(pt, x);///这时候,x就成了父亲节点,pt就是子节点
  }
}
int main() {
  cin >> n;
  _Init();
  for (int i = 1; i <= n; i++) cin >> a[i];
  /// 输入当前节点的权值
  /// n-1条边
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    _Add(u, v);
    _Add(v, u);///无向图双向建边
  }
  DFS(1, -1);
  dfs(1, -1);
  ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, pre[i]);
  }
  cout << ans << endl;
  return 0;
}
/**
8
9 4 1 7 10 1 6 5
1 2
2 3
1 4
1 5
5 6
5 7
5 8
121
**/


目录
相关文章
|
7月前
Elasticsearch【问题记录 02】【不能以root运行es + max virtual memory areas vm.max_map_count [65530] is too low处理】
【4月更文挑战第12天】Elasticsearch【问题记录 02】【不能以root运行es + max virtual memory areas vm.max_map_count [65530] is too low处理】
62 3
|
7月前
|
前端开发
[1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
43 0
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
184 0
CF1550B Maximum Cost Deletion(分段比较)
CF1550B Maximum Cost Deletion(分段比较)
41 0
|
Go
LeetCode 124. Binary Tree Maximum Path Sum
给定一个非空二叉树,返回其最大路径和。 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列。该路径至少包含一个节点,且不一定经过根节点。
70 0
LeetCode 124. Binary Tree Maximum Path Sum
|
算法
LeetCode 321. Create Maximum Number
给定长度分别为 m 和 n 的两个数组,其元素由 0-9 构成,表示两个自然数各位上的数字。现在从这两个数组中选出 k (k <= m + n) 个数字拼接成一个新的数,要求从同一个数组中取出的数字保持其在原数组中的相对顺序。 求满足该条件的最大数。结果返回一个表示该最大数的长度为 k 的数组。
73 0
LeetCode 321. Create Maximum Number
LeetCode 222. Count Complete Tree Nodes
给出一个完全二叉树,求出该树的节点个数。
91 0
LeetCode 222. Count Complete Tree Nodes
|
vr&ar
CF482B. Interesting Array(线段树)
CF482B. Interesting Array(线段树)
65 1
CF1454 E. Number of Simple Paths (基环树 拓扑排序)
CF1454 E. Number of Simple Paths (基环树 拓扑排序)
94 0
成功解决lightgbm.basic.LightGBMError: Parameter max_depth should be of type int, got “0.02“
成功解决lightgbm.basic.LightGBMError: Parameter max_depth should be of type int, got “0.02“