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 个数全都加上 ...
随机推荐
- Go web表单验证
开发Web的一个原则就是,不能信任用户输入的任何信息,所以验证和过滤用户的输入信息就变得非常重要 必填字段 if len(r.Form["username"][0])==0{ // ...
- JavaScript之this解析
1.解析器在调用函数每次都会向函数内部传递进一个隐含的参数,这个隐含的参数就是this,this指向的是一个对象,这个对象我们称为函数执行的上下文对象,根据函数的调用方式不同,this会指向不同的对象 ...
- 在amazon linux上安装Jenkins
原文请参考: https://medium.com/@itsmattburgess/installing-jenkins-on-amazon-linux-16aaa02c369c
- maven中settings文件的配置
鉴于上一个博客写的maven打包,读者老爷们可能找不到settings文件的配置,这里专门附上settings文件的配置: 有的settings文件只含有一些空标签,需要手动去配置. <?xml ...
- Git的使用规范(二)
今天,我们来介绍一下git的一些命令行,来帮我们后面可以面对一些情况的时候,我们可以有一些解决的方法 1.git查看历史记录最全的命令行 git log --pretty=raw 2.对于一下的几个情 ...
- Uber:中国市场两年内不考虑盈利,每年补贴10亿美金
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- FreeRTOS任务暂停和启动函数
任务句柄 TaskHandle_t pump_task_handle = NULL; 任务的启动函数 if(eTaskGetState(pump_task_handle) != eRunning) v ...
- FPGA数字鉴相鉴频器的开发记录
1. 对于电机的锁相控制,需要对相差进行PI性质的环路滤波,但现有的锁相环中鉴频鉴相器输出为相差脉冲而非数字量,难以直接进行PI特性的环路滤波. 通过对晶振的非整数分频获取准确的参考时钟,基于触发器机 ...
- 转MySQL详解--索引
写在前面:索引对查询的速度有着至关重要的影响,理解索引也是进行数据库性能调优的起点.考虑如下情况,假设数据库中一个表有10^6条记录,DBMS的页面大小为4K,并存储100条记录.如果没有索引,查询将 ...
- oracle 开启归档日志模式
摘自:https://www.jianshu.com/p/f8c0e9309ce2 在默认情况下,oracle数据库是在非归日志档模式中创建的,在非归档日志模式中,进行日志切换时会直接重写redo l ...