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 ...
随机推荐
- 【刷题】BZOJ 4289 PA2012 Tax
Description 给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点1到点N的最小代价.起点的代价是离开起点的边的边权,终点的代价是进入终点的边的边 ...
- emWin notes
emwin 学习小记 @2018-7-5 ## 在使用 graph 控件时,需要在坐标上显示波形,波形刷新函数 GRAPH_DATA_YT_AddValue() 放在一个 while(1) 中,这样便 ...
- c++ 使用PI
#define _USE_MATH_DEFINES #include<cmath> 加上上面这个就可以使用 M_PI 了,不行就使用下面这个: #define _USE_MATH_DEFI ...
- debian8.4 ubuntu14.04双系统_debian8.4硬盘安装
本文根据官网说明和实际安装而写,利在分享知识,但转载请注明出处以尊重知识! 一 首先在win7下利用EasyBCD添加安装选项,“添加新条目|安装|配置” 在弹出的memu.lst中填写如下内容: t ...
- 20145215卢肖明《网络对抗》逆向及Bof基础
20145215卢肖明<网络对抗>逆向及Bof基础 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任 ...
- sklearn5_preprocessing数据标准化
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...
- Python案例
我感觉好方啊,Python和C语言不一样啊....写了几个例子... 变量 变量的占位后面没有逗号啊啊啊啊 name='张泉' age=22 tel=110 print ('姓名:%s,年龄:% ...
- Hadoop Yarn源码 - day2
接着昨天的继续看hadoop-yarn-api,昨天看了api package下的4个协议,今天来看下con package下的代码 conf目录下的内容比较少,就4个文件分别是Configurati ...
- bzoj千题计划208:bzoj3174: [Tjoi2013]拯救小矮人
http://www.lydsy.com/JudgeOnline/problem.php?id=3174 按a+b从小到大排序,a+b小的在上面,先考虑让它逃出去 正确性不会证 感性理解一下,最后一个 ...
- Chrome插件LiveStyle结合Sublime Text3编辑器实现高效可视化开发
LiveStyle是Chrome中提高开发效率的一款CSS编辑器插件.利用LiveStyle和Sublime Text3编辑器结合可实现可视化开发,一次配置,简单易用! 本文由前端交流QQ群管理员—— ...