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的更多相关文章

  1. codeforces 812E Sagheer and Apple Tree(思维、nim博弈)

    codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...

  2. CodeForces 812E Sagheer and Apple Tree 树上nim

    Sagheer and Apple Tree 题解: 先分析一下, 如果只看叶子层的话. 那么就相当于 经典的石子问题 nim 博弈了. 那我们看非叶子层. 看叶子层的父亲层. 我们可以发现, 如果从 ...

  3. cf202-div 1-B - Apple Tree:搜索,数论,树的遍历

      http://codeforces.com/contest/348/problem/B   B. Apple Tree time limit per test 2 seconds memory l ...

  4. POJ 2486 Apple Tree

    好抽象的树形DP......... Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6411 Accepte ...

  5. poj 3321:Apple Tree(树状数组,提高题)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 5629 Descr ...

  6. poj 3321 Apple Tree dfs序+线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outsid ...

  7. [poj3321]Apple Tree(dfs序+树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26762   Accepted: 7947 Descr ...

  8. POJ 3321 Apple Tree(树状数组)

                                                              Apple Tree Time Limit: 2000MS   Memory Lim ...

  9. 【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 ...

随机推荐

  1. 【BZOJ1306】[CQOI2009]循环赛(搜索)

    [BZOJ1306][CQOI2009]循环赛(搜索) 题面 BZOJ 洛谷 题解 爆搜一下,\(hash\)记录是否已经考虑过这个状态,记忆化解决问题. #include<iostream&g ...

  2. oracle数据库连接池查看

    select username , count(*), machine from v$session where username is not null group by username, mac ...

  3. linux socat命令

    http://note.youdao.com/noteshare?id=35901183d9ccc09632339ec971fa58dd

  4. 个推数据统计产品(个数)iOS集成实践

    最近业务方给我们部门提了新的需求,希望能一站式统计APP的几项重要数据.这次我们尝试使用的是个推(之前专门做消息推送的)旗下新推出的产品“个数·应用统计”,根据官方的说法,个推的数据统计产品通过专业的 ...

  5. SQL Server 中ROW_NUMBER() OVER基本用法

    1.不能排序法 * FROM table1 WHERE id NOT IN ( SELECT TOP 开始的位置 id FROM table1 ) 2.SQL 2000 临时表法 DECLARE @S ...

  6. 管理KVM虚拟机(二)

    管理KVM虚拟机 工具:libvirt 官网:http://libvirt.org/ 介绍:Libvirt 库是一种实现 Linux 虚拟化功能的 Linux® API,它支持各种虚拟机监控程序,包括 ...

  7. Linux shell 日期,时间相关的命令

    在shell脚本中,经常要用到跟获取日期相关的东西,这里记录一下Linux shell 获取日期的方法 获取当前日期:today=`date +"%Y-%m-%d"` 获取昨天的日 ...

  8. SqlParameter防止SQL注入

    SQL注入的解决方案有好几种,待我细细研究过之后逐一讲解. 方法一:SqlParameter方法 这里有一篇博客是详细介绍SqlParameter的,可以看看 点我 string sqlStr=&qu ...

  9. 51Nod 1684 子集价值 (平方和去括号技巧)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1684 题意: 新建一个位运算,求所有子集通过这个位运算后的答案的平方和是 ...

  10. 用MFC(C++)实现拼音搜索

    2015年4月1日更新: 我在github开源了Objective-C版的拼音搜索项目,感兴趣的可以去看看: OC版拼音搜索 最近项目需要实现按照拼音搜索资源.在网上找了一下,这方面的东西太少了. J ...