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 Tree with Maximum Cost---CF1092F 树上DP_权值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:

Tree with Maximum Cost---CF1092F 树上DP_权值_02

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定义为 Tree with Maximum Cost---CF1092F 树上DP_权值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
**/