Apple Tree
题意:
给一有根树,每个叶子上有一些苹果,现在要求你拿掉一些苹果,使得每一个点的 儿子的子树内的苹果数相同。
解法:
首先可以发现$cnt$个叶子节点之间的关系可以用$cnt-1$个独立方程表示出来。
这样相当于在方程的解中只有一个变元。
接下来求出最小整数基底:这个我们可以两遍$dfs$ + $gcd$求出。
总效率$O(nlogn)$
#include <iostream>
#include <cstdio>
#include <cstring> #define LL long long
#define N 100010
#define INF 0x3f3f3f3f3f3f3f3fLL using namespace std; struct edge
{
int x,to;
}E[N<<]; int n,totE;
int g[N];
LL f[N],a[N],sum;
bool is_leaf[N]; void addedge(int x,int y)
{
E[++totE] = (edge){y,g[x]}; g[x]=totE;
E[++totE] = (edge){x,g[y]}; g[y]=totE;
} LL gcd(LL a,LL b)
{
if(!b) return a;
return gcd(b,a%b);
} #define p E[i].x LL calc(int x,int tp)
{
int cnt=;
LL ans=;
for(int i=g[x];i;i=E[i].to)
if(p!=tp)
{
LL tmp = calc(p,x);
cnt++;
if(ans/gcd(ans,tmp)<=sum/tmp)
ans = ans/gcd(ans,tmp)*tmp;
else return -;
}
if(!cnt)
{
is_leaf[x]=;
return 1LL;
}
if(ans<=sum/cnt) return ans*(LL)cnt;
else return -;
} void dfs(int x,int tp)
{
int cnt=;
for(int i=g[x];i;i=E[i].to)
if(p!=tp) cnt++;
for(int i=g[x];i;i=E[i].to)
if(p!=tp)
{
f[p]=f[x]/(LL)cnt;
dfs(p,x);
}
} int main()
{
while(~scanf("%d",&n))
{
for(int i=;i<=n;i++) g[i]=,is_leaf[i]=;
totE=;
sum=;
for(int i=;i<=n;i++)
scanf("%I64d",&a[i]),sum+=a[i];
for(int i=,x,y;i<n;i++)
{
scanf("%d%d",&x,&y);
addedge(x,y);
}
f[]=calc(,);
if(f[]==-)
{
cout << sum << endl;
continue;
}
dfs(,);
LL t=INF, ans=;
for(int i=;i<=n;i++)
if(is_leaf[i])
{
ans += a[i];
t = min(t, a[i]/f[i]);
}
ans -= t*f[];
cout << ans << endl;
}
return ;
} /*
t*A_i <= B_i(原先的)
t <= B_i/A_i
*/
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 ...
- Apple Tree(需要预处理的树状数组)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20335 Accepted: 6182 Descr ...
随机推荐
- Intel® RAID Software Users Guide
Intel® RAID Software Users Guide: Intel ® Embedded Server RAID Technology 2 Intel ® IT/IR RAID I ...
- (转)Java web 项目中文件路径
文件路径分为绝对路径和相对路径,在项目中页面跳转.配置文件读写.文件上传下载等等许多地方都涉及到文件路径问题. 一篇好文转载于此:http://blog.csdn.net/shendl/archive ...
- 模式识别之不变矩---SIFT和SURF的比较
- jquery 通过ajax 提交表单
1.需要引入以下两个js文件 <script src="Easyui/jquery-1.7.2.min.js"></script> <scrip ...
- ASP.NET MVC4中ViewBag、ViewData和TempData的使用和区别
一.说明 本文章主要是讲解asp.net mvc中ViewBag.ViewData和TempData的使用和区别,ViewBag.ViewData和TempData常常用于将action方法中的数据传 ...
- mongodb学习之:副本集
前面一张介绍了主从模式,现在mongoDB官方已经不建议使用主从模式了,替代方案是采用副本集的模式.副本集不能在一台电脑上操作.需要准备3台电脑进行搭建.副本集就是mongoDB副本所组成的一个集群. ...
- xcode环境变量设置(转载)
一般我们在xcode里面配置包含工程目录下头文件的时候,都要关联着相对路径和绝对路径,如果只是自己用这个项目,用绝对路径的问题不大,但是如果你把工程发给别人,别人就要在改这个绝对路径,这时候绝对路径的 ...
- Javascript高级程序设计笔记(很重要尤其是对象的设计模式与继承)
var obj = {'a':'a'}; var fun = function (){} console.log(typeof obj);//object console.log(typeof fun ...
- escape() VS encodeURI() VS encodeURIComponent()
JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decod ...
- hdu1052 田忌赛马 —— 贪心
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1052 错误代码: #include<stdio.h>//田忌赛马,错误版 #include ...