Codeforces 348B - Apple Tree
我们设最后答案为 x , 我们我们就能用x表示出所有节点下面的苹果个数, 然后用叶子节点求lcm, 取最大的可行解。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long
using namespace std; const int N = 1e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; int n, a[N];
LL dv[N], sum[N], all;
vector<int> leaf;
vector<int> G[N]; void solve(int u, int fa) {
if(u != && SZ(G[u]) == ) leaf.push_back(u);
sum[u] = a[u];
for(int v : G[u]) {
if(v == fa) continue;
solve(v, u);
sum[u] += sum[v];
}
} void dfs(int u, int fa) {
if(dv[u] > all) {
printf("%lld\n", all);
exit();
}
for(int v : G[u]) {
if(v == fa) continue;
if(u == ) dv[v] = dv[u] * SZ(G[u]);
else dv[v] = dv[u] * (SZ(G[u]) - );
dfs(v, u);
}
} bool check(LL x) {
for(auto& id : leaf) {
if(x / dv[id] > a[id]) return false;
}
return true;
}
int main() {
scanf("%d", &n);
for(int i = ; i <= n; i++) scanf("%d", &a[i]), all += a[i];
for(int i = ; i < n; i++) {
int u, v; scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dv[] = ;
solve(, );
dfs(, );
LL lcm = ;
for(auto& id : leaf) {
LL gcd = __gcd(lcm, dv[id]);
if(1.0 * lcm / gcd * dv[id] > all) {
printf("%lld\n", all);
return ;
}
lcm = lcm / gcd * dv[id];
}
LL low = , high = all / lcm, ans = ;
while(low <= high) {
LL mid = low + high >> ;
if(check(mid * lcm)) ans = mid, low = mid + ;
else high = mid - ;
}
printf("%lld\n", all - ans * lcm);
return ;
} /*
*/
Codeforces 348B - Apple Tree的更多相关文章
- codeforces 812E Sagheer and Apple Tree(思维、nim博弈)
codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...
- CodeForces 812E Sagheer and Apple Tree 树上nim
Sagheer and Apple Tree 题解: 先分析一下, 如果只看叶子层的话. 那么就相当于 经典的石子问题 nim 博弈了. 那我们看非叶子层. 看叶子层的父亲层. 我们可以发现, 如果从 ...
- cf202-div 1-B - Apple Tree:搜索,数论,树的遍历
http://codeforces.com/contest/348/problem/B B. Apple Tree time limit per test 2 seconds memory l ...
- 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 ...
随机推荐
- MT【90】图论基础知识及相关例题
此讲适合参加全国联赛二试的同学 介绍图论和我们学习的一般的知识点比如函数一样,首先要介绍一些定义,只是图论里的定义相对较多,这里给出部分在竞赛中常用到的: 就像学函数的时候,学了定义和相关概念后我们要 ...
- 安利一个很火的 Github 滤镜项目
安利一个很火的 Github 滤镜项目 园长 1 个月前 简评:通过深度学习,一秒钟让你的照片高大上,这是康奈尔大学和 Adobe 的工程师合作的一个新项目,通过卷积神经网络把图片进行风格迁移.项目已 ...
- 学习6__STM32--SPI外设之中断收发---
<目标> STM32双机 SPI中断收发通信 <描述> # STM32双机配置为一主一从模式 # 采用主机中断发送,从机中断接收 # 收发机制采用不间断收发(发送为空就发送,接 ...
- spring.net框架配置和使用
spring.net框架学习笔记 spring.net框架是用于解决企业应用开发的复杂性的一种容器框架,它的一大功能IOC(控制反转),通俗解释就是通过spring.net框架的容器创建对象实体,而不 ...
- maven的三种工程pom、jar、war的区别
转: maven的三种工程pom.jar.war的区别 2018年09月03日 10:02:53 houjx3 阅读数:2918更多 个人分类: maven 1.pom工程:用在父级工程或聚合工程 ...
- Spring整合Quartz定时任务 在集群、分布式系统中的应用(Mysql数据库环境)
Spring整合Quartz定时任务 在集群.分布式系统中的应用(Mysql数据库环境) 转载:http://www.cnblogs.com/jiafuwei/p/6145280.html 单个Q ...
- hdu 2433 Travel
http://acm.hdu.edu.cn/showproblem.php?pid=2433 题意: 求删除任意一条边后,任意两点对的最短路之和 以每个点为根节点求一个最短路树, 只需要记录哪些边在最 ...
- spring+spring mvc+JdbcTemplate 入门小例子
大家使用这个入门时候 最好能够去 搜一下 spring mvc 的 原理,我放一张图到这里,自己琢磨下,后面去学习就容易了 给个链接 (网上一把,千万不能懒) https://www.cnblo ...
- Training (deep) Neural Networks Part: 1
Training (deep) Neural Networks Part: 1 Nowadays training deep learning models have become extremely ...
- Brief History of Machine Learning
Brief History of Machine Learning My subjective ML timeline Since the initial standpoint of science, ...