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的更多相关文章

  1. Codeforces Round #563 (Div. 2) F. Ehab and the Big Finale

    后续: 点分治标程 使用father数组 比使用vis数组优秀(不需要对vis初始化) https://codeforces.com/problemset/problem/1174/F https:/ ...

  2. 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 题意: 给出一个 ...

  3. 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 ...

  4. 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任选 ...

  5. Codeforces Round #525 (Div. 2) E. Ehab and a component choosing problem 数学

    题意:给出树 求最大的sigma(a)/k k是选取的联通快个数   联通快不相交 思路: 这题和1个序列求最大的连续a 的平均值  这里先要满足最大平均值  而首先要满足最大  也就是一个数的时候可 ...

  6. Codeforces Round #525 (Div. 2)B. Ehab and subtraction

    B. Ehab and subtraction 题目链接:https://codeforc.es/contest/1088/problem/B 题意: 给出n个数,给出k次操作,然后每次操作把所有数减 ...

  7. 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,找 ...

  8. 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/ ...

  9. Codeforces Round #525 (Div. 2) C. Ehab and a 2-operation task

    传送门 https://www.cnblogs.com/violet-acmer/p/10068786.html 题意: 给定一个长度为 n 的数组a[ ],并且有两种操作: ①将前 i 个数全都加上 ...

随机推荐

  1. ctf题目writeup(7)

    2019.2.10 过年休息归来,继续做题. bugku的web题,地址:https://ctf.bugku.com/challenges 1. http://123.206.87.240:8002/ ...

  2. Kuernetes-设计架构(二)

    Kubernetes设计架构 Kubernetes集群包含有节点代理kubelet和Master组件(APIs,scheduler.etc),一切都基于分布式的存储系统.Kubernetes架构图: ...

  3. win7 下安装oracle 11g出现错误: 启动服务出现错误 找不到服务OracleMTSRecoveryService

    这种错误是在多次安装oracle都没有成功的情况下发生的. 正确安装oracle,是有前提条件的 1,安装最新的jdk,不是jre!!(并配好环境变量,在cmd中测试 java -version与ja ...

  4. python语法join函数

    Python语法中join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串. vid = )

  5. python 基础篇 15 内置函数和匿名函数

    ------------------------>>>>>>>>>>>>>>>内置函数<<< ...

  6. C++中的默认参数规则

    C++中的默认参数规则 C++的默认参数规则其实是一个非常容易掉坑的规则,尤其是当一个函数拥有多个声明的时候,每个声明的默认参数可以各不相同,在调用时又可能与每个声明都不同:这篇博客稍微列举一下C++ ...

  7. python——pyinstaller生成exe基本使用和遇到的坑

    1.安装 pip install pyinstaller 2.常规操作 在cmd界面(之前安装python或者anaconda的时候正确添加环境变量的话,是可以在cmd界面直接执行pyinstalle ...

  8. VS2010历史记录清理

    把如下粘贴到文本文件里,另存为批处理文件.(后缀为 *.bat)双击执行就可 @echo off cd \ @echo on @REG Delete HKEY_CURRENT_USER\Softwar ...

  9. HDU 1398 Square Coins 整数拆分变形 母函数

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit ...

  10. Java 中的异常和处理详解(转载)

    原文出处: 代码钢琴家 简介 程序运行时,发生的不被期望的事件,它阻止了程序按照程序员的预期正常执行,这就是异常.异常发生时,是任程序自生自灭,立刻退出终止,还是输出错误给用户?或者用C语言风格:用函 ...