Codeforces Round #525 (Div. 2) F. Ehab and a weird weight formula
F. Ehab and a weird weight formula
题目链接:https://codeforces.com/contest/1088/problem/F
题意:
给出一颗点有权值的树,满足只有一个点的权值最小,然后除开这个点,每个点都有一个权值比它更小的点与之相邻。
然后要求你重构这颗树,满足点权及边权和最小。
点权计算方法: au = au*num(num为与之相邻边的个数);
边权计算方法: e{u,v},we = dis(u,v)*min(au,av) (dis(u,v)为given tree中u和v的距离)。
题解:
首先我们若以权值最小的点为根,我们会发现这棵树越往下,点的权值就会越大。
假定我们随便选择一对{u,v},那么对答案的贡献就是 au+av+log2(dis(u,v))*min(au,av),要让权值最小,假设我们先固定u来寻找v,由于受到v的权值和dis(u,v)的限制,所以我们可以考虑采用贪心的思想,对于一个点u,我们尽可能地向其祖先找点v,同时算一下距离。最终会找到一个最优解。
但是直接枚举太慢了,发现首先我们可以优化一下,就是每次往上找2的倍数个结点(因为题目中是log2(dis(u,v)) )。
由于av<au,所以每对{u,v}对答案的贡献就是au+(log2(dis(u,v))+1)*av。
最终总复杂度是O(nlogn)。
代码如下:
#include <bits/stdc++.h>
#define INF 999999999999
using namespace std; typedef long long ll ;
const int N =5e5+ ;
int a[N],dp[][N];
int n,st;
vector <int> vec[N];
ll ans;
void dfs(int u,int pa){
dp[][u]=pa;
for(int i=;i<;i++){
if(dp[i-][u]==-) break ;
dp[i][u]=dp[i-][dp[i-][u]];
}
ll minx = INF;
int i;
for(i=;i<;i++){
if(dp[i][u]==-) break ;
minx=min((ll)(i+)*a[dp[i][u]]+a[u],minx);
}
minx=min((ll)(i+)*a[st]+a[u],minx);
if(u!=st) ans+=minx;
for(auto v:vec[u]){
if(v!=pa) dfs(v,u);
}
}
int main(){
scanf("%d",&n);
st=;a[]=1e9+;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]<a[st])st=i;
}
for(int i=,u,v;i<n;i++){
scanf("%d%d",&u,&v);
vec[u].push_back(v);
vec[v].push_back(u);
}
memset(dp,-,sizeof(dp));
dfs(st,-);
printf("%I64d",ans);
return ;
}
Codeforces Round #525 (Div. 2) F. Ehab and a weird weight formula的更多相关文章
- Codeforces Round #563 (Div. 2) F. Ehab and the Big Finale
后续: 点分治标程 使用father数组 比使用vis数组优秀(不需要对vis初始化) https://codeforces.com/problemset/problem/1174/F https:/ ...
- Codeforces Round #525 (Div. 2)E. Ehab and a component choosing problem
E. Ehab and a component choosing problem 题目链接:https://codeforces.com/contest/1088/problem/E 题意: 给出一个 ...
- Codeforces Round #525 (Div. 2)D. Ehab and another another xor problem
D. Ehab and another another xor problem 题目链接:https://codeforces.com/contest/1088/problem/D Descripti ...
- Codeforces Round #525 (Div. 2) C. Ehab and a 2-operation task 数学 mod运算的性质
C. Ehab and a 2-operation task 数学 mod运算的性质 题意: 有两种对前缀的运算 1.对前缀每一个\(a +x\) 2.对前缀每一个\(a\mod(x)\) 其中x任选 ...
- Codeforces Round #525 (Div. 2) E. Ehab and a component choosing problem 数学
题意:给出树 求最大的sigma(a)/k k是选取的联通快个数 联通快不相交 思路: 这题和1个序列求最大的连续a 的平均值 这里先要满足最大平均值 而首先要满足最大 也就是一个数的时候可 ...
- Codeforces Round #525 (Div. 2)B. Ehab and subtraction
B. Ehab and subtraction 题目链接:https://codeforc.es/contest/1088/problem/B 题意: 给出n个数,给出k次操作,然后每次操作把所有数减 ...
- Codeforces Round #525 (Div. 2)A. Ehab and another construction problem
A. Ehab and another construction problem 题目链接:https://codeforc.es/contest/1088/problem/A 题意: 给出一个x,找 ...
- Codeforces Round #525 (Div. 2) D. Ehab and another another xor problem(待完成)
参考资料: [1]:https://blog.csdn.net/weixin_43790474/article/details/84815383 [2]:http://www.cnblogs.com/ ...
- Codeforces Round #525 (Div. 2) C. Ehab and a 2-operation task
传送门 https://www.cnblogs.com/violet-acmer/p/10068786.html 题意: 给定一个长度为 n 的数组a[ ],并且有两种操作: ①将前 i 个数全都加上 ...
随机推荐
- 2-Linux C语言指针与内存-学习笔记
Linux C语言指针与内存 前面我们对于: c语言的基本用法 makeFile文件的使用 main函数的详解 标准输入输出流以及错误流管道 工具与原理 指针与内存都是c语言中的要点与难点 指针 数组 ...
- R语言绘图:词云图
使用wordcloud2绘制词云图 library(wordcloud2) findwords<-function(tf){ txt<-scan(tf,"") wl&l ...
- 杭电 1003 Max Sum (动态规划)
参考:https://www.cnblogs.com/yexiaozi/p/5749338.html #include <iostream> #include <cstdio> ...
- Ubuntu14.04安装opencv2.4.13
本文参考相关链接:http://blog.csdn.net/honyniu/article/details/46390097 系 统:Ubuntu 14.04 x64 opencv版本:2.4.1 ...
- CPU计算密集型和IO密集型
CPU计算密集型和IO密集型 第一种任务的类型是计算密集型任务,其特点是要进行大量的计算,消耗CPU资源,比如计算圆周率.对视频进行高清解码等等,全靠CPU的运算能力.这种计算密集型任务虽然也可以用多 ...
- 加载旋转框(loading spinner)
目标是这样的 用到的组件 AlertDialog 和 ProgressBar 先创建一个 AlertDialog 的布局 <?xml version="1.0" encodi ...
- MAVEN的项目升级
今天我们来介绍一下版本依赖的问题 1.如果是admin的话,他要依赖于service的版本,则service的版本依赖于core的版本, 如果是本地编译,这我直接更新admin的就可以了,然后直接跑就 ...
- WPF中的线程使用
原文:WPF中的线程使用 简介 但凡涉及到图形界面,往往的设计都是不支持或者不推荐使用多个线程操作界面内容.而且通常会有一个专门的线程调度器来处理任务线程和界面线程的问题.下面提供两个两个方案. 使用 ...
- 一种简单实用的双向电平转换电路3.3V-5V
当你使用3.3V的单片机的时候,电平转换就在所难免了,经常会遇到3.3转5V或者5V转3.3V的情况,这里介绍一个简单的电路,他可以实现两个电平的相互转换(注意是相互哦,双向的,不是单向的!).电路十 ...
- 实用脚本 1 -- 安装Ctags
Ctags是vim下方便代码阅读的工具,一般VIM中已经默认安装了Ctags,它可以帮助程序员很容易地浏览源代码. 1.如果系统中没有此工具用如下方法安装: 到ctags官网下载源码,解压后 ...