题意:

给你一棵无根树,每个节点有个权值$a_i$,指定一个点u,定义$\displaystyle value = \sum^v a_i*dist(u,v)$,求value的最大值

n,ai<=2e5

思路:

其实就是找一个节点作为根满足上述最大的value

直接枚举是$O(n^2)$的,肯定不行,我们要用到换根法

换根适用于这种无根树找根,两个跟直接产生的结果又有联系,可以相互转换的情况

对于这一题,我们让sum[u] = 以u为根的子树的$\sum a_i$

这样,从父亲节点u向儿子节点v转移的时候,

假设此时的value(整棵树以u为根)为res,我们要将res的值转化为以v为根的value

大前提:此时u是整棵树的根!         //没有这个大前提也可以,你要预处理一下每个节点祖先的$\sum a_i$,然后在下面的操作中搞一下,但是我们完全可以通过只改变sum[u],sum[v]的值来决定到底谁才是整棵树的根,因为无论u,v谁是根,其他节点的sum[]都是不变的!嘻嘻

首先$value_v$相比$value_u$,根(v或u)与以v为根的子树中的每一个节点的距离都小了1

在value上表现为 res -= sum[v]

其次在以v为根的子树之外的节点,跟到那些节点的距离都大了1

所以sum[u] -= sum[v], res += sum[u]

此时因为v要成为整个树的根,所以sum[v]+=sum[u]

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional> #define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = 1e9+;
const int maxn = 2e6+;
const int maxm = 2e6+;
const int inf = 0x3f3f3f3f;
const db pi = acos(-1.0); vector<int>g[maxn];
int a[maxn];
ll res, ans;
ll sum[maxn]; void dfs(int x, int fa, int h){
int sz = g[x].size();
res += 1ll*h*a[x];
sum[x] = a[x];
for(int i = ; i < sz; i++){
if(g[x][i] == fa)continue;
dfs(g[x][i], x, h+);
sum[x] += sum[g[x][i]];
}
return;
} void dfs2(int x, int fa){
ans = max(res, ans);
int sz = g[x].size();
for(int i = ; i < sz; i++){
int y = g[x][i];
if(y == fa) continue; res -= sum[y];
sum[x] -= sum[y];
res += sum[x];
sum[y] += sum[x]; dfs2(y, x); sum[y] -= sum[x];
res -= sum[x];
sum[x] += sum[y];
res += sum[y];
}
return;
} int main(){
int n;
scanf("%d", &n);
mem(sum, );
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
}
for(int i = ; i < n; i++){
int x, y;
scanf("%d %d",&x,&y);
g[x].pb(y);
g[y].pb(x);
}
res = ;
ans = ;
dfs(,-,);
dfs2(,-);
printf("%lld", ans);
return ;
} /* */

明天(今天)还得磨锤子,赶紧睡觉了

Codeforces 1092 F Tree with Maximum Cost (换根 + dfs)的更多相关文章

  1. Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS换根 || 树形dp】

    传送门:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 sec ...

  2. Codeforces Round #527 (Div. 3) . F Tree with Maximum Cost

    题目链接 题意:给你一棵树,让你找一个顶点iii,使得这个点的∑dis(i,j)∗a[j]\sum dis(i,j)*a[j]∑dis(i,j)∗a[j]最大.dis(i,j)dis(i,j)dis( ...

  3. Codeforces Round #527 F - Tree with Maximum Cost /// 树形DP

    题目大意: 给定一棵树 每个点都有点权 每条边的长度都为1 树上一点到另一点的距离为最短路经过的边的长度总和 树上一点到另一点的花费为距离乘另一点的点权 选定一点出发 使得其他点到该点的花费总和是最大 ...

  4. CF F - Tree with Maximum Cost (树形DP)给出你一颗带点权的树,dist(i, j)的值为节点i到j的距离乘上节点j的权值,让你任意找一个节点v,使得dist(v, i) (1 < i < n)的和最大。输出最大的值。

    题目意思: 给出你一颗带点权的树,dist(i, j)的值为节点i到j的距离乘上节点j的权值,让你任意找一个节点v,使得dist(v, i) (1 < i < n)的和最大.输出最大的值. ...

  5. Codeforces 1092F Tree with Maximum Cost(树形DP)

    题目链接:Tree with Maximum Cost 题意:给定一棵树,树上每个顶点都有属性值ai,树的边权为1,求$\sum\limits_{i = 1}^{n} dist(i, v) \cdot ...

  6. CF1092 --- Tree with Maximum Cost

    CF1324 --- Maximum White Subtree 题干 You are given a tree consisting exactly of \(n\) vertices. Tree ...

  7. E. Tree Painting(树形换根dp)

    http://codeforces.com/contest/1187/problem/E 分析:问得分最高,实际上就是问以哪个节点出发得到的分数最多,而呈现成代码形式就变成了换根,max其得分!!!而 ...

  8. 2018.12.19 codeforces 1092F. Tree with Maximum Cost(换根dp)

    传送门 sbsbsb树形dpdpdp题. 题意简述:给出一棵边权为1的树,允许选任意一个点vvv为根,求∑i=1ndist(i,v)∗ai\sum_{i=1}^ndist(i,v)*a_i∑i=1n​ ...

  9. codeforces#1187E. Tree Painting(树换根)

    题目链接: http://codeforces.com/contest/1187/problem/E 题意: 给出一颗树,找到一个根节点,使所有节点的子节点数之和最大 数据范围: $2 \le n \ ...

随机推荐

  1. 10.Python中print函数中中逗号和加号的区别

    先看看print中逗号和加号分别打印出来的效果.. 这里以Python3为例 1 print("hello" + "world") helloworld 1 p ...

  2. 用户svn密码自定义

    由于在linux系统Apache+svn服务器,用户需要自定义密码怎么办呢? 1.创建脚本目录 mkdir -p /var/www/svn/svntools 2.创建apache配置文件 touch ...

  3. css常用单词

    <!-- type = circle表示空心圆 -->     <!-- type = disc 表示实心圆 -->     <!-- type = square表示方块 ...

  4. CTPN训练自己的数据集过程大白话记录

    一.算法理解 此处省略1万字.............. 二.训练及源码理解 配置以下3步: 在utils文件夹和utils\bbox文件夹下创建__init__.py文件 在utils\bbox文件 ...

  5. 前端Tips#4 - 用 process.hrtime 获取纳秒级的计时精度

    本文同步自 JSCON简时空 - 前端Tips 专栏#4,点击阅读 视频讲解 视频地址 文字讲解 如果去测试代码运行的时长,你会选择哪个时间函数? 一般第一时间想到的函数是 Date.now 或 Da ...

  6. SEATA 分布式事务入门DEMO

    Simple Extensible Autonomous Transacation Architecture,seata是简单的.可扩展.自主性高的分布式架构 SEATA Server Configu ...

  7. python小功能记录

    本博客会不断完善,记录python小功能. 1. 合并两个字典 # in Python 3.5+ >>> x = {'a': 1, 'b': 2} >>> y = ...

  8. spring boot的日常配置

    配置篇 #数据库连接配置msql spring.datasource.url:jdbc:mysql://127.0.0.1:3306/test spring.datasource.username: ...

  9. ZJCTF预赛一个.py的逆向题

    #!/usr/bin/env python # -*- coding:utf-8 -*- from hashlib import sha256 def xor(a,b): result = [] fo ...

  10. influxdb的命令们

    InfluxDB是一个开源的时序数据库,使用GO语言开发,特别适合用于处理和分析资源监控数据这种时序相关数据.而InfluxDB自带的各种特殊函数如求标准差,随机取样数据,统计数据变化比等,使数据统计 ...