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. python 排列组合

    笛卡尔积(product): 假设集合A={a, b},集合B={0, 1, 2},则两个集合的笛卡尔积为{(a, 0), (a, 1), (a, 2), (b, 0), (b, 1), (b, 2) ...

  2. ADB工具的安装

    1.Windows ADB工具下载地址: https://developer.android.google.cn/studio/releases/platform-tools ADB工具官网教程: h ...

  3. Python3爬虫(二)网络爬虫的尺寸与约束

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.网络爬虫的尺寸: 1.小规模,数据量小,爬取速度不敏感,Requests库,爬取网页 2.中规模,数据规模较大 ...

  4. C++ vector二维数组

    C++ 构建二维动态数组 int **p; p = ]; //注意,int*[10]表示一个有10个元素的指针数组 ; i < ; ++i) { p[i] = ]; } 这样就构成10*5的数组 ...

  5. HDU暑假多校第六场K-werewolf

    一.题意 好人必然说真话,坏人不一定说真话,给定N个人的言论<每人一个发言.不谈及自己>,要求指出有多少个人一定是好人,有多少个人一定是坏人.#define 狼人 坏人#define 村民 ...

  6. octave简易操作

    语言以分号;结尾if for while等语句后用,来承接关系if ,elseif ,else ,end;for i=1:10,end;while a>3 ,end;   while true, ...

  7. ionic打包apkFailed to execute shell command "input,keyevent,82"" on device: Error: adb: Command failed with exit code 137

    错误代码如下 BUILD SUCCESSFUL in 12s 46 actionable tasks: 1 executed, 45 up-to-date Built the following ap ...

  8. Grok Debugger本地安装(转载)

    原文链接:http://fengwan.blog.51cto.com/508652/1758845 最近在使用ELK对日志进行集中管理,因为涉及到日志的规则经常要用到http://grokdebug. ...

  9. Linux - 信息收集

    1. #!,代表加载器(解释器)的路径,如: #!/bin/bash echo "Hello Boy!" 上面的意思是说,把下面的字符(#!/bin/bash以下的所有字符)统统传 ...

  10. 【MVC】 小问题

    [MVC] 小问题 1. url 传参中文乱码 : encodeURIComponent 转码 2. RedirectToAction 重定向 : ajax 调用无效, 直接 url 访问有效 3. ...