Codeforces Round #527 (Div. 3)-F. Tree with Maximum Cost-树上换根

本文介绍了一种使用树形动态规划(Tree DP)的方法来解决寻找树上节点配置以达到最大路径成本的问题。通过定义状态和转移方程,文章详细解释了如何计算以每个节点作为根时树的整体花费,并找出最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

codeforce-1092-F. Tree with Maximum Cost-树上换根

【Description】

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 avassigned 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=1ndist(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≤2105).

The second line of the input contains nintegers a1,a2,,an (1≤ai≤2105), where ai is 
the value of the vertex i

Each of the next n−1lines 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】

Sample Input
8
9 4 1 7 10 1 6 5
1 2
2 3
1 4
1 5
5 6
5 7
5 8
Sample Output
121
Sample Input
1
1337
Sample Output
0

【Problem Description】

给你一棵树,树上每个节点的权值为a[i],定义dist(x,y)为x到y的边数。
再定义一个节点v,v为树上任意的一个节点,定义这棵树的花费为val=dist(i,v)*a[i],i从1到n。
求这棵树的最大花费。

【Solution】

img

树形dp
定义sum[i]为以i为根节点的子树的权值和(包括i),dp[i]为以i为根节点的整棵树的花费。
假设已知sum[]数组和dp[1]的值,那么我们可以求得根节点1的所有直接子节点的值dp[j]。

对于上图假设j=5,则dp[5]=dp[1]+(sum[1]-sum[5])-sum[5]
其中sum[1]-sum[5]=a[1]+a[2]+a[3]+a[4],sum[5]=a[5]+a[6]+a[7]+a[8];
因为对于1234这四个节点来说,当根节点从1换为5时,他们到根节点的dist都增加了1,所以对于整棵树来说
花费增加了sum[1]-sum[5];
对于5678这四个节点来说类似,当根节点从1换为5时,他们到根节点的dist都减少了1,所以对于整棵树来说
花费减少了sum[5]

至于sum数组和dp[1]可以通过dfs直接求得。

【Code】

/*
 * @Author: Simon 
 * @Date: 2019-01-16 21:58:37 
 * @Last Modified by: Simon
 * @Last Modified time: 2019-01-16 22:18:33
 */
#include<bits/stdc++.h>
using namespace std;
typedef int Int;
#define int long long
#define INF 0x3f3f3f3f
#define maxn 200005
int a[maxn],dp[maxn],sum[maxn];//节点权值,dp[i]表示以i为根节点的花费,sum[i]表示以i为根节点的子树的权值和
vector<int>g[maxn];//邻接表
int dfs1(int u,int p){//处理出sum数组和dp[1]
    sum[u]=a[u];
    for(auto v:g[u]){
        if(v!=p){
            dfs1(v,u);
            sum[u]+=sum[v];
            dp[u]+=dp[v]+sum[v];
        }
    }
}
int ans=dp[1];
int dfs2(int u,int p){//从根节点开始,对于其每个子节点,进行换根
    for(auto v:g[u]){
        if(v!=p){
            dp[v]=dp[u]+(sum[1]-sum[v])-sum[v];
            dfs2(v,u);
        }
    }
    // cout<<sum[1]<<' '<<u<<' '<<dp[u]<<endl;
    ans=max(ans,dp[u]);
}
Int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;scanf("%lld",&n);
    for(int i=1;i<=n;i++){
        scanf("%lld",a+i);
    }
    for(int i=1;i<n;i++){
        int u,v;scanf("%lld%lld",&u,&v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs1(1,-1); dfs2(1,-1);
    printf("%lld\n",ans);
    cin.get(),cin.get();
    return 0;
}
### 关于 Codeforces Round 839 Div 3 的题目与解答 #### 题目概述 Codeforces Round 839 Div 3 是一场面向不同编程水平参赛者的竞赛活动。这类比赛通常包含多个难度层次分明的问题,旨在测试选手的基础算法知识以及解决问题的能力。 对于特定的比赛问题及其解决方案,虽然没有直接提及 Codeforces Round 839 Div 3 的具体细节[^1],但是可以据以往类似的赛事结构来推测该轮次可能涉及的内容类型: - **输入处理**:给定一组参数作为输入条件,这些参数定义了待解决的任务范围。 - **逻辑实现**:基于输入构建满足一定约束条件的结果集。 - **输出格式化**:按照指定的方式呈现最终答案。 考虑到提供的参考资料中提到的其他几场赛事的信息[^2][^3],可以推断出 Codeforces 圆桌会议的一般模式是围绕着组合数学、图论、动态规划等领域展开挑战性的编程任务。 #### 示例解析 以一个假设的例子说明如何应对此类竞赛中的一个问题。假设有如下描述的一个简单排列生成问题: > 对于每一个测试案例,输出一个符合条件的排列——即一系列数字组成的集合。如果有多种可行方案,则任选其一给出即可。 针对上述要求的一种潜在解法可能是通过随机打乱顺序的方式来获得不同的合法排列形式之一。下面是一个 Python 实现示例: ```python import random def generate_permutation(n, m, k): # 创建初始序列 sequence = list(range(1, n + 1)) # 执行洗牌操作得到新的排列 random.shuffle(sequence) return " ".join(map(str, sequence[:k])) # 测试函数调用 print(generate_permutation(5, 2, 5)) # 输出类似于 "4 1 5 2 3" ``` 此代码片段展示了怎样创建并返回一个长度为 `k` 的随机整数列表,其中元素取自 `[1..n]` 这个区间内,并且保证所有成员都是唯一的。需要注意的是,在实际比赛中应当仔细阅读官方文档所提供的精确规格说明,因为这里仅提供了一个简化版的方法用于解释概念。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值