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. Buffer.alloc()

    Buffer.alloc(size[, fill[, encoding]]) Node.js FS模块方法速查 size {Number} fill {Value} 默认:undefined enco ...

  2. C语言学习6

    int i; 定义整形变量i int *p;  p为指向整型数据的指针变量 int a[n]: 定义整形数组a,他有n个元素 int *p[n]:   定义指针数组p,它有n个指向整型数据的指针元素组 ...

  3. poj 1088 滑雪 DP(dfs的记忆化搜索)

    题目地址:http://poj.org/problem?id=1088 题目大意:给你一个m*n的矩阵 如果其中一个点高于另一个点 那么就可以从高点向下滑 直到没有可以下滑的时候 就得到一条下滑路径 ...

  4. PAT 1077. 互评成绩计算

    PAT 1077. 互评成绩计算 在浙大的计算机专业课中,经常有互评分组报告这个环节.一个组上台介绍自己的工作,其他组在台下为其表现评分.最后这个组的互评成绩是这样计算的:所有其他组的评分中,去掉一个 ...

  5. 九度oj 题目1068:球的半径和体积

    题目1068:球的半径和体积 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6148 解决:2269 题目描述: 输入球的中心点和球上某一点的坐标,计算球的半径和体积 输入: 球的中心点和 ...

  6. Light oj-1100 - Again Array Queries,又是这个题,上次那个题用的线段树,这题差点就陷坑里了,简单的抽屉原理加暴力就可以了,真是坑~~

                                                                              1100 - Again Array Queries ...

  7. hdu 3691最小割将一个图分成两部分

    转载地址:http://blog.csdn.net/xdu_truth/article/details/8104721 题意:题给出一个无向图和一个源点,让你求从这个点出发到某个点最大流的最小值.由最 ...

  8. Jquery根据JSON生成Table

    先说下背景 本人属于juqery小白中的极品小白.基本对于JS jquery这些不懂.用到时候基本百度下 拿过来改改OK. 上面这东西让我弄了三天.可能对于其他人来说 一天就搞定了 .看来还真得去学一 ...

  9. Add Two Numbers(链表)

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  10. JAVA生成扫描条形码

    条形码是一种可视化.机器可读的数据,这些数据通常描述了携带该条码的物品的相关信息.条形码已经广泛被应用在商品流通,图书管理,邮政管理和银行系统等领域.在这篇文章中,将介绍如何生成和扫描一些常见的一维和 ...