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 ...
随机推荐
- 斯坦福公开课:Developing IOS 8 App with Swift(1-3)心得体会
最近开始学习Swift开发移动程序.跟随斯坦福大学的公开课进行自学. 这真是一个美好的时代,虽然不能在斯坦福求学,但是可以观看录制的授课录像.讲义,好似老师在给我们上课一样! 心得: 1.每节课信息量 ...
- linux查看磁盘信息
linux查看磁盘挂载信息:df -hlinux查看文件夹大小:sudo du -h --max-depth=1
- Oracle 一行拆分为多行
测试数据: CREATE TABLE t (str VARCHAR2(30)); INSERT INTO t VALUES ( 'X,Y,Z' ); INSERT INTO t VALUES ( 'X ...
- ffmpeg下载rtmp flv
ffmpeg -i rtmp://shanghai.chinatax.gov.cn:1935/fmsApp/16a0148f117.flv -c copy dump.flv
- scikit-learn:4.2. Feature extraction(特征提取,不是特征选择)
http://scikit-learn.org/stable/modules/feature_extraction.html 带病在网吧里. ..... 写.求支持. .. 1.首先澄清两个概念:特征 ...
- aapt命令获取apk具体信息(包名、版本号号、版本号名称、兼容api级别、启动Activity等)
aapt命令获取apk具体信息(包名.版本号号.版本号名称.兼容api级别.启动Activity等) 第一步:找到aapt 找到sdk的根文件夹,然后找到build-tools文件夹.然后会看到一些b ...
- "Installing Software" has encountered a problem---pydev on ubuntu
"Installing Software" has encountered a problem. An error occurred while collecting items ...
- 原来浏览器原生支持JS Base64编码解码 outside of the Latin1 range
原来浏览器原生支持JS Base64编码解码 « 张鑫旭-鑫空间-鑫生活 https://www.zhangxinxu.com/wordpress/2018/08/js-base64-atob-bto ...
- linux mount的安装配置
1.首先需要安装nfs-utils 和 portmap: yum install nfs-utils portmap (安装软件,也可直接安装 yum install nfs* portmap)chk ...
- VK Cup 2012 Qualification Round 1 E. Phone Talks —— DP
题目链接:http://codeforces.com/contest/158/problem/E E. Phone Talks time limit per test 3 seconds memory ...