https://www.zybuluo.com/ysner/note/1300249

题面

给一棵大小为\(n\)的树,树的每个叶子节点上有权值。

定义一颗树平衡:对于每一个结点\(u\)的子树都拥有相同的权值之和。

问至少要减掉多少权值才能使树平衡。

  • \(n\leq 10^5\)

解析

这题想了半天。。。

一开始的想法是\(dfs\)一遍,回溯时每个点把其所有子树减到其最小子树大小。

然而立即发现我是个**,这会影响子树内的平衡。

如果把一个点的权值定义为它子树内的权值和,

在一个子树内,所有的点都减去同一个值,才不会影响其平衡。

所以如果要在一个点减小其权值,一减就要减它所有儿子权值的\(lcm\)。

为了使当前点平衡,我们需要使其所有子树大小相等。

为了使以后的修改可行,当然要使所有子树大小都为\(lcm\)的倍数。

如果一个儿子大小小于\(lcm\),说明不能保留子树。

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define ll long long
#define re register
#define il inline
#define pb(a) push_back(a)
#define fp(i,a,b) for(re int i=a;i<=b;i++)
#define fq(i,a,b) for(re int i=a;i>=b;i--)
using namespace std;
const int N=1e5+100;
int n,m,h[N],cnt,tag=1;
ll w[N],ans,sz[N],dp[N],s;
struct Edge{int to,nxt;}e[N<<1];
il void add(re int u,re int v){e[++cnt]=(Edge){v,h[u]};h[u]=cnt;}
il int gi()
{
re int x=0,t=1;
re char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') t=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*t;
}
ll lcm(re ll a,re ll b)
{
if(a*b>1e18+5e17) return tag=0,1;
return a/__gcd(a,b)*b;
}
il void dfs(re int u,re int fa)
{
dp[u]=w[u],sz[u]=1;re int Son=0;
for(re int i=h[u];i+1;i=e[i].nxt)
{
re int v=e[i].to;
if(v==fa) continue;
++Son;
dfs(v,u);
sz[u]=lcm(sz[u],sz[v]);
dp[u]+=dp[v];
}
for(re int i=h[u];i+1;i=e[i].nxt)
{
re int v=e[i].to;
if(v==fa) continue;
dp[u]=min(dp[u],dp[v]-dp[v]%sz[u]);
}
if(!Son) Son=1;
dp[u]*=Son;sz[u]*=Son;
}
int main()
{
memset(h,-1,sizeof(h));
n=gi();
fp(i,1,n) w[i]=gi(),s+=w[i];
fp(i,1,n-1)
{
re int u=gi(),v=gi();
add(u,v);add(v,u);
}
dfs(1,0);
printf("%lld\n",s-dp[1]);
return 0;
}

[CF348B]Apple Tree的更多相关文章

  1. POJ 2486 Apple Tree

    好抽象的树形DP......... Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6411 Accepte ...

  2. poj 3321:Apple Tree(树状数组,提高题)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 5629 Descr ...

  3. poj 3321 Apple Tree dfs序+线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outsid ...

  4. [poj3321]Apple Tree(dfs序+树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26762   Accepted: 7947 Descr ...

  5. POJ 3321 Apple Tree(树状数组)

                                                              Apple Tree Time Limit: 2000MS   Memory Lim ...

  6. 【HDU 4925】BUPT 2015 newbie practice #2 div2-C-HDU 4925 Apple Tree

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/C Description I’ve bought an or ...

  7. POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25904   Accepted: 7682 Descr ...

  8. URAL 1018 Binary Apple Tree(树DP)

    Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a bina ...

  9. POJ3321 Apple Tree (树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16180   Accepted: 4836 Descr ...

随机推荐

  1. Centos6.5下 执行“ll”提示“-bash: ll: command not found”

    ll 是 ls -l的别名,之所所以 ll出现错误是因为没有定义别名. 如果要实现ll 命令,可以做如下操作: 编辑 ~./bashrc 添加 ls -l 的别名为 ll即可 [root@Centos ...

  2. python多线程和多进程(一)

    一.多线程 Python的标准库提供了两个模块:_thread和threading,_thread是低级模块,threading是高级模块,对_thread进行了封装.绝大多数情况下,只需要使用thr ...

  3. 分布式集群环境下运行Wordcount程序

    1.分布式环境的Hadoop提交作业方式与本地安装的Hadoop作业提交方式相似,但有两点不同: 1)作业输入输出都存储在HDFS 2)本地Hadoop提交作业时将作业放在本地JVM执行,而分布式集群 ...

  4. Java基础学习总结(89)——为什么单元测试应该我们开发人员来写

    软件测试是为了保证项目质量,单元测试可以快速执行测试回归测试,做好单元测试可以大大提升测试效率,项目开发真正达到敏捷效果. 单元测试做什么? 1. 核心类方法 2. 异常处理 3. 边界值测试 4. ...

  5. JavaEE JDBC 事务

    JDBC 事务 @author ixenos 事务 1.概念:我们将一组语句构建成一个事务(trans action),当所有语句顺利执行之后,事务可以被提交(commit):否则,如果其中某个语句遇 ...

  6. python-gzip解压缩(实验吧SOS)

    本题看着很简单,就是在弄出来的老是乱码,看了pcat的wp还是不行,下面的评论说可能是python版本问题,改版本太麻烦,试了一下先gzip解压,得到的文件在打开就不是乱码了,代码如下: # -*- ...

  7. MTK TP手势添加

    old: #include "tpd.h" #include "tpd_custom_gt9xx.h" #ifndef TPD_NO_GPIO #include ...

  8. Linux下汇编语言学习笔记65 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  9. Rikka with Phi 线段树

    Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds ...

  10. 一 hadoop 相关介绍

    hadoop 相关介绍 hadoop的首页有下面这样一段介绍.对hadoop是什么这个问题,做了简要的回答. The Apache™ Hadoop® project develops open-sou ...