[CF348B]Apple Tree
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的更多相关文章
- POJ 2486 Apple Tree
好抽象的树形DP......... Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6411 Accepte ...
- poj 3321:Apple Tree(树状数组,提高题)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18623 Accepted: 5629 Descr ...
- poj 3321 Apple Tree dfs序+线段树
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Description There is an apple tree outsid ...
- [poj3321]Apple Tree(dfs序+树状数组)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26762 Accepted: 7947 Descr ...
- POJ 3321 Apple Tree(树状数组)
Apple Tree Time Limit: 2000MS Memory Lim ...
- 【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 ...
- POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25904 Accepted: 7682 Descr ...
- 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 ...
- POJ3321 Apple Tree (树状数组)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16180 Accepted: 4836 Descr ...
随机推荐
- odoo 权限配置讲解
今天来讲解一下odoo权限配置的简单讲解,配合公司开发的权限模块的使用,进行odoo权限配置的说明 BaseSecurityExtend 2.0模块 这是公司自主开发的一款针对odoo菜单级别进行可视 ...
- MyBatis 多参问题
当传入的参数为多个参数时 1 可以不封装为Javabean直接传入,写法如下 public List<XXXBean> getXXXBeanList(String xxId, String ...
- 3.2.8 sed 的运作
sed 的工作方式相当直接.命令行上的每个文件会依次打开与读取.如果没有文件,则使用标准输入,文件名“-”(单个破折号)可用于表示标准输入. [many@avention Desk ...
- maven运行出现错误:Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[]](xjl456852原创)
maven在使用tomcat插件tomcat7-maven-plugin:2.2:run运行项目,出现下面错误: 严重: A child container failed during start j ...
- source insight中的快捷键总结
1.快捷键 1,Shift+F8高亮显示指定字符. 2,Ctrl+F找出来的结果用F4,F3前进后退查找. 3,Alt+,后退alt+.前进查找关键字. 4,Alt+G或者F5跳转到某个固定的行号. ...
- 04003_CSS
1.DIV相关的技术 (1)DIV是一个html标签,一个块级元素(单独显示一行).它单独使用没有任何意义,必须结合CSS来使用,主要用于页面的布局: (2)Span是一个html标签,一个内联元素( ...
- jQuery学习之------html()、text()和val()
jQuery学习之------html().text()和val() .html(),.text()和.val()的差异总结: (来源:慕课网) .html(),.text(),.val()三种方法 ...
- 九度oj 题目1525:子串逆序打印
题目1525:子串逆序打印 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:3124 解决:530 题目描述: 小明手中有很多字符串卡片,每个字符串中都包含有多个连续的空格,而且这些卡片在 ...
- WebLoad 解析服务器返回的XML格式内容
Parsing the XML Response get the root node: var rootNode = document.wlXmls[0].XMLDocument.documentE ...
- noi.openjudge——8465 马走日
http://noi.openjudge.cn/ch0205/8465/ 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 1024kB 描述 马在中国象棋以日字形规则移动. 请编写 ...