[codeforces1092F]Tree with Maximum Cost

版权声明:辛辛苦苦码字,你们转载的时候记得告诉我 https://blog.csdn.net/dxyinme/article/details/85255873

time limit per test : 2 seconds
memory limit per test : 256 megabytes

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

Let d i s t ( x , y ) dist(x,y) be the distance between the vertices x x and y 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 v . Then the cost of the tree is i = 1 n d i s t ( i , v ) a i \sum^{n}_{i=1} dist(i,v)⋅a_i .

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

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

The second line of the input contains n
integers a 1 , a 2 , , a n ( 1 a i 2 1 0 5 ) a1,a2,…,an (1≤a_i≤2⋅10^5) , where a i a_i is the value of the vertex i i .

Each of the next n 1 n−1 lines describes an edge of the tree. Edge i i is denoted by two integers ui and vi, the labels of vertices it connects ( 1 u i , v i n , u i v i ) . (1≤u_i,v_i≤n, u_i≠v_i).

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 v
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

Input

1
1337

Output

0

Note

Picture corresponding to the first example:
在这里插入图片描述
You can choose the vertex 3 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 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 0 .

题意:
给一个 n n 个点的树,每个点有一个点权 a i a_i ,, d i s t ( x , y ) dist(x,y) 表示 x x y y 的简单路径长度,求一点 v v 使得 i = 1 n d i s t ( i , v ) a i \sum^{n}_{i=1} dist(i,v)⋅a_i 最大,答案输出最大值。

题解:
考虑已知以一个点为 v v 点的答案,求其移动到一个相邻的点对答案造成的影响。假设当前 v v 点为 x x 点,当前的答案为 n o w now ,要将 v v 设为与 x x 相邻的 y y 点,则答案变化为 n o w v a l [ y ] + s u m a v a l [ y ] now-val[y]+suma-val[y] v a l [ y ] val[y] 是以 y y 为根的子树的所有点点权之和。

#include<bits/stdc++.h>
#define LiangJiaJun main
#define ll long long
using namespace std;
int n,cnt,ne,h[200004],a[200004];
struct edge{
    int to,nt;
}e[400004];
ll suma,ans;
bool vis[200004];
ll dis[200004],val[200004];
void add(int u,int v){
     e[++ne].to=v;e[ne].nt=h[u];
     h[u]=ne;
}
void dfs(int x){
     ans+=a[x]*(dis[x]-1);
     for(int i=h[x];i;i=e[i].nt){
         if(dis[e[i].to])continue;
         dis[e[i].to]=dis[x]+1;
         dfs(e[i].to);
         val[x]+=val[e[i].to];
     }
     val[x]+=a[x];
}
void calc(int x,ll now){
     ans=max(ans,now);
     vis[x]=1;
     for(int i=h[x];i;i=e[i].nt){
         if(vis[e[i].to])continue;
         calc(e[i].to,now+suma-(val[e[i].to]<<1));
     }
}
int w33ha(){
    ne=0;suma=0;
    memset(val,0,sizeof(val));
    memset(dis,0,sizeof(dis));
    memset(h,0,sizeof(h));
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        suma+=a[i];
    }
    for(int i=1;i<n;i++){
        int u,v;scanf("%d%d",&u,&v);
        add(u,v);add(v,u);
    }
    ans=0;
    dis[1]=1;
    dfs(1);
    calc(1,ans);
    printf("%lld\n",ans);
    return 0;
}
int LiangJiaJun(){
    while(scanf("%d",&n)!=EOF)w33ha();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/dxyinme/article/details/85255873