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. java基础day05---界面

    java基础day05---界面 1.GUI:图形用户界面(Graphics User Interface) 开发工具包AWT抽象窗口把工具箱===>swing 解决了awt存在的lcd问题== ...

  2. Python: 列表的两种遍历方法

    方法一:以列表中元素的下标进行访问 def traverse1(list1): for i in range(len(list1)): print(list1[i], end=' ') print() ...

  3. TFS权限配置

            装了TFS,要给TFS里添加用户,然后分配权限.其实一般项目中权限都不会控制的那么细,所以就直接想给项目组的每个人建一个用户,让他们都能访问这个项目的代码并进行任何操作.只想怎么简单怎 ...

  4. c++ constructor, copy constructor, operator =

    // list::push_back #include <iostream> #include <list> class element{ private: int numbe ...

  5. Java与C++进行系统间交互:Protocol Buffer

    在一次项目中,因笔者负责的java端应用需要与公司C++系统进行交互,公司选定Protocol Buffer方案,故简单的了解一下 有需要的可以看一下其他作者的文章,了解一下Protobuf: htt ...

  6. 基于HTML5移动web应用

    一.基于HTML5移动web应用 1.canvas 绘图 2.多媒体 3.本地存储 4.离线应用 5.使用地理位置 6.移动web框架   二.具体说明 1.HTML5标准最大的变化就是支持Web绘图 ...

  7. matlab mex 小o -o 出错

    https://github.com/kyamagu/mexopencv/issues/117 就是说2014a以后的版本mex   -o 选项变成了 -output 蛋疼,这有什么好改的.找了好久才 ...

  8. linux消息队列通信

    IPC机制 进程间通信机制(Inter Process Communication,IPC),这些IPC机制的存在使UNIX在进程通信领域手段相当丰富,也使得程序员在开发一个由多个进程协作的任务组成的 ...

  9. [转]如何清空Chrome缓存和Cookie

    当您使用浏览器(例如 Chrome)时,浏览器会将网站中的一些信息保存在其缓存和 Cookie 中. 清除这些内容可以解决某些问题,例如网站上的加载或格式设置问题. 在 Chrome 中 在计算机上打 ...

  10. python字符串排序方法

    一般情况下,python中对一个字符串排序相当麻烦: 一.python中的字符串类型是不允许直接改变元素的.必须先把要排序的字符串放在容器里,如list. 二.python中的list容器的sort( ...