[CF348B]Apple Tree
2024-09-05 23:23:28
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;
}
最新文章
- 原生JS 年月日、省市区 三级联动
- jquery实现自动补全邮箱地址
- go语言 rune切片
- JabRef 文献管理软件
- [LeetCode]题解(python):125 Valid Palindrome
- mysql integer size 大小
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState解决?
- Redis的Set操作
- Poj2948Martian Mining(记忆化)
- python的资料
- ECSTORE AJAX提交的实现
- 调用QQ截图
- Java集合中的AbstractMap抽象类
- expect 批量自动部署ssh 免密登陆
- 基于js的数据结构与算法-数组
- html复习小结
- android ndk-build 编译静态库libxx.a 以及Android studio openssl 静态库配置(cmake)
- Python中的get和set方法
- Python学习一|anaconda的安装问题以及Python语言的特点
- linux 监控性能学习笔记(1)
热门文章
- C#基础学习(一)
- ubuntu14.04 Google Chrome can not be run as root
- ArcGIS:Hello World Maps
- <;input type=";button"; />; 和<;input type=";submit"; />; 的区别
- POJ1222熄灯问题【位运算+枚举】
- 1004. 成绩排名 (20) (快速排序qsort函数的使用问题)
- Rsync文件同步服务器配置
- 567. Permutation in String
- 莫比乌斯反演套路三、四--BZOJ2154: Crash的数字表格 &;&; BZOJ2693: jzptab
- 使用流的方式去进行post请求解决中文乱码问题返回xml格式