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 ...
随机推荐
- mac osx 下编译 OpenWrt
默认的文件系统hfs大小写不敏感.新建一个磁盘镜像文件并合式化为hfs+, 然后挂载到系统中. hdiutil create -size 20g -fs "Case-sensitive HF ...
- IOS版App的控件元素定位
前言 Android版App的控件元素可以通过Android studio自带的工具uiautomatorviewer来协助定位! IOS版App的控件元素可以通过Appium来实现(未实现),或ap ...
- LeetCode(15)题解--3Sum
https://leetcode.com/problems/3sum/ 题目: Given an array S of n integers, are there elements a, b, c i ...
- ssh key 生成
1.设置好git的name和email $ git config --global user.name "姓名" $ git config --global user.email ...
- 使用JavaScript获取浏览器UserAgent
可以在浏览器地址栏输入about:version来查看UserAgent等信息 但是在Win10系统,本人亲测,IE和Edge用这样的方式都获取不到信息 在我惯用的QQ浏览器上倒是可以获取到 为了能方 ...
- tomcat服务器配置java堆内存大小
我用的是绿色免安装的tomcat,找到tomcat下的bin文件夹下的catalina.bat文件: 编辑该文件,编辑参数,没有的话手动加上: set JAVA_OPTS=-server -Xms51 ...
- 云计算系列——HIVE1.2.1 - JDBC 服务
前提 Hadoop 集群已经启动 Hive1.2.1 环境已经搭建 一.启动 HIVE - JDBC 服务 hiveserver2 为 hive 的 jdbc 服务,此服务默认为前台进程,需要在执行 ...
- PAT 天梯赛 L1-050. 倒数第N个字符串 【字符串】
题目链接 https://www.patest.cn/contests/gplt/L1-050 思路 因为是求倒数 我们不如直接 倒过来看 令 zzz 为第一个字符串 我们可以理解为 十进制 转换为 ...
- shell动态变量
面对变量中嵌套变量,可以这么做 other_devops_ip="......." options='_ip' tennat_name='other_devops' tennat_ ...
- CentOS 下源码安装LAMP环境
一.简介 什么是LAMP LAMP是一种Web网络应用和开发环境,是Linux, Apache, MySQL, Php/Perl的缩写,每一个字母代表了一个组件,每个组件就其本身而言都是在它所代 ...