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 ...
随机推荐
- 洛谷SP16549 QTREE6 - Query on a tree VI(LCT)
洛谷题目传送门 思路分析 题意就是要维护同色连通块大小.要用LCT维护子树大小就不说了,可以看看蒟蒻的LCT总结. 至于连通块如何维护,首先肯定可以想到一个很naive的做法:直接维护同色连通块,每次 ...
- C语言常用修饰符
前言 这两天在梳理自己C语言的知识,发现写了这么久的代码,居然所有的知识点都在自己的脑袋里.这可不好,万一老了呢.... 接下来的几天里,会以文字的形式,将这些知识整理出来,分享给大家. 想要看看英文 ...
- springboot中定时任务
import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.context.annotat ...
- 【BZOJ3745】Norma(CDQ分治)
[BZOJ3745]Norma(CDQ分治) 题面 BZOJ 洛谷 题解 这种问题直接做不好做,显然需要一定的优化.考虑\(CDQ\)分治. 现在唯一需要考虑的就是跨越当前中间节点的所有区间如何计算答 ...
- 一步步创建第一个Docker App —— 4. 部署应用
原文:https://docs.docker.com/engine/getstarted-voting-app/deploy-app/ 在这一步中,将会使用第一步提到的 docker-stack.ym ...
- IntelliJ IDEA(2018)安装详解
转: IntelliJ IDEA(2018)安装详解 置顶 2018年06月06日 22:58:45 Lazymanx 阅读数:95701 版权声明: https://blog.csdn.net/ ...
- hdu 6166 Senior Pan
http://acm.hdu.edu.cn/showproblem.php?pid=6166 题意: 给出一张无向图,给定k个特殊点 求这k个特殊点两两之间的最短路 二进制分组 枚举一位二进制位 这一 ...
- 前端 ajax 改写登录界面
SSM 整合项目开发到一个阶段,想慢慢地把前台框架等技术引入进来 突然碰到一个困惑好久的问题: ajax 替换原本 form 表单 post 提交登录: 一直 404 错误,心塞.... 最后发现原来 ...
- scale.fix.js
无意间在一个网站上看到的,本来是对另一个效果感兴趣的,结果看到这个放开来的js就读了一下. var metas = document.getElementsByTagName('meta'); var ...
- 关于python开发CRM系统
注意本项目是针对培训学校开发简化的CRM CRM简介 CRM全称:customer relationship management 无CRM的痛点 没有CMR的缺点及痛点: 每个销售会通过Excel来 ...