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 ...
随机推荐
- 关于mysql engine(引擎)的疑问
http://bbs.chinaunix.net/thread-989698-1-1.html
- git clean
使用git clean清除未加入版控的数据 作者:Level Up 发布日期:2012-12-21 10:48:10 笔者在使用版本控制软件时,不知为何常常会有些暂存的数据产生.像是下面 ...
- 轻松搞定RabbitMQ(六)——主题
转自 http://blog.csdn.net/xiaoxian8023/article/details/48806871 翻译地址:http://www.rabbitmq.com/tutorials ...
- (全然背包)小P寻宝记——好基友一起走
题目描写叙述 话说.上次小P到伊利哇呀国旅行得到了一批宝藏.他是相当开心啊.回来就告诉了他的好基友小鑫.于是他们又结伴去伊利哇呀国寻宝. 这次小P的寻宝之路可没有那么的轻松,他们走到了一个森林,小鑫一 ...
- python--面向对象封装
from collectoins import namedtuple Point=namedtuple('point',['x','y']) t1=Point(1,2) print(t1.x) pri ...
- 九度OJ 1118:数制转换 (进制转换)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3873 解决:1494 题目描述: 求任意两个不同进制非负整数的转换(2进制-16进制),所给整数在long所能表达的范围之内. 不 ...
- VCL里的构造函数
好奇,为什么Create函数明明是个构造函数,还要带上override;这是C++里没有的事情.我虽然也明白其大致的作用和目的,但还是没有见到官方和权威的说法.如果哪位大大见到此文,还望给一个详细一点 ...
- 【Effective C++】资源管理
资源:动态分配的内存.文件描述器.互斥锁.图形界面中的字型与笔刷.数据库连接以及网络sockets等,无论哪一种资源,重要的是,当你不再使用它时,必须将它还给系统. 条款13:以对象管理资源 当我们向 ...
- 关于4Ps 、4Cs 、4Rs 、4Vs营销策略的内容及优劣比较
- 剑指Offer:树的子结构【26】
剑指Offer:树的子结构[26] 题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) 解题思路 分为两步: 第一步:在树A中找到和树B的根节点的值一 ...