Codeforces 349D - Apple Tree
来源:互联网 发布:三类医疗器械软件 编辑:程序博客网 时间:2024/05/01 13:34
有一个有根树,每个叶子节点都有一定量的苹果(可能为0),非叶子节点上没有苹果。一个节点的重量定义为在以这个节点为根的子树内的节点的苹果个数和。对于一个节点称它是平衡的当且仅当这个节点的每个子节点的重量都相等。问最小需要拿走多少个苹果才能令所有节点都平衡。
假设我们已经将一个子树拿成了平衡的,这个子树的父节点需要这个子树继续拿走苹果的时候,苹果是一份一份拿的,一份中含有的苹果的个数依赖于这个子树的形态。手动模拟几次后会发现,某个节点的一份苹果个数等于他这个点的子节点的一份苹果个数的
那么对于一个节点,先计算这个节点的可拿走的苹果个数,然后计算苹果个数的上界。然后贪心的选择一个最大的重量更新状态来更新节点的情况。自底向上计算答案即可。
#include<bits/stdc++.h>using namespace std;#define LL long long const int maxn = 112345;vector<int> edge[maxn];const LL INFF = 0x3f3f3f3f3f3f3f3f;LL val[maxn],dp[maxn],csub[maxn];bool hav;LL lcm(LL a,LL b){ if(a * b > INFF){ hav = false; return 1; } LL gcd = __gcd(a,b); return a / gcd * b;}void dfs(int st,int fa){ dp[st] = val[st], csub[st] = 1 ; int ssiz = 0; for(auto x : edge[st]){ if(x == fa) continue; ssiz ++; dfs(x,st); csub[st] = lcm(csub[st],csub[x]); dp[st] += dp[x]; } for(auto x : edge[st]){ if(x == fa) continue; dp[st] = min(dp[st],dp[x] - dp[x] % csub[st]); } if(ssiz == 0) ssiz = 1; dp[st] *= ssiz; csub[st] *= ssiz;}int main(){ int n; scanf("%d",&n); LL sum = 0; for(int i = 1;i<=n;i++){ scanf("%I64d",&val[i]); sum += val[i]; } int l,r; for(int i = 1;i<n;i++){ scanf("%d %d",&l,&r); edge[l].push_back(r); edge[r].push_back(l); } hav = true; dfs(1,0); if(!hav) dp[1] = 0; printf("%I64d\n",sum - dp[1]); return 0;}
0 0
- Codeforces 349D - Apple Tree
- CodeForces-349D Apple Tree(树型dp)
- CodeForces 349D Apple Tree(树形DP)
- Codeforces Round #202 (Div. 2) D. Apple Tree (DFS + Math)
- codeforces D. Distance in Tree
- Codeforces 343D Water Tree
- CodeForces 343D Water Tree
- codeforces 343D - Water Tree
- Codeforces 343D Water Tree
- Codeforces 570 D. Tree Requests
- codeforces 570 D Tree Requests
- Codeforces 570D Tree Requests
- Codeforces 343D Water Tree
- CODEFORCES 343D. Water Tree
- codeforces-675D Tree Construction
- CodeForces 343D Water Tree
- 【CodeForces】343D Water Tree
- codeforces 570D Tree Requests
- Android HTTPDES 原理解析
- react native 遇到的问题总结
- 面向对象 (多态)+JAVA学习笔记-DAY09
- 安卓项目依赖
- Memcache常用命令
- Codeforces 349D - Apple Tree
- 安卓httprul post方式上传中文乱码问题解决方案
- Android 百度地图开发(一)--- 申请API Key和在项目中显示百度地图
- android读取工程根目录下文件内容
- android6.0运行时权限处理简介
- 请问下./在Linux下是什么意思
- 如何提交代码到github
- Android开发与调试一:adb在win7下安装与使用
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用