题意:

给一有根树,每个叶子上有一些苹果,现在要求你拿掉一些苹果,使得每一个点的 儿子的子树内的苹果数相同。

解法:

首先可以发现$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的更多相关文章

  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 ...

  10. Apple Tree(需要预处理的树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20335   Accepted: 6182 Descr ...

随机推荐

  1. 【Access2007】Access2007的打开方式

    Access2007提供了多种打开方式 仅仅读与非仅仅读就不用说了,就是能编辑与不可以编辑的差别 是否以独占的方式打开是Access2007的打开方式的核心 这里什么都没有写的打开是指以"共 ...

  2. oracle insert/update

    insert into table (,,) values (,,) where update table set (,)=(select , from ) where

  3. __del__删除方法

    class dog: def __del__(self): print("删除机制被调用了...") dog1 = dog() del dog1 #在这里是先删除掉了,所以就去上面 ...

  4. 基于django做增删改查组件,分页器组件

    增删改查组件 一.Djangoadmin的启发 二.基于Djangoadmin实现数据的增删改查 分页器组件 分页器组件的介绍以及源码解读 补充:源码下载,

  5. ORACLE 查看表结构

    select table_name from user_tables; //当前用户的表 select table_name from all_tables; //所有用户的表 select tabl ...

  6. poj 1163 The Triangle &amp;poj 3176 Cow Bowling (dp)

    id=1163">链接:poj 1163 题意:输入一个n层的三角形.第i层有i个数,求从第1层到第n层的全部路线中.权值之和最大的路线. 规定:第i层的某个数仅仅能连线走到第i+1层 ...

  7. C++代码书写模板 -- 如何判断函数类型

    先说一个简单的方案. 经过验证 g++ 和 vs2010 都可以.原理就是利用函数类型可以直接转换成函数指针. template<class T> bool test( T * t ) { ...

  8. The server must be started under an unprivileged user ID to prevent

    mysql8 PostgreSQL [root@test local]# postgres -D /usr/local/pgsql/data"root" execution of ...

  9. 初识代码封装工具SWIG(回调Python函数)

    这不是我最早使用swig了,之前在写Kynetix的时候就使用了swig为python封装了C语言写的扩展模块.但是当时我对C++还不是很了解,对其中的一些概念也只是拿来直接用,没有理解到底是什么,为 ...

  10. NSArray / NSSet / NSDictory 三者的异同点

    NSArray / NSSet / NSDictory 三者的异同点 NSArray 是一个有序对象的一个集合.相当于一个队列存储,可以有重复的数进去. NSSet 比较典型的一个HASH表(集合)算 ...