CodeForces-349D Apple Tree(树型dp)
来源:互联网 发布:onese刻录dvd数据 编辑:程序博客网 时间:2024/05/01 18:18
You are given a rooted tree with n vertices. In each leaf vertex there's a single integer — the number of apples in this vertex.
The weight of a subtree is the sum of all numbers in this subtree leaves. For instance, the weight of a subtree that corresponds to some leaf is the number written in the leaf.
A tree is balanced if for every vertex v of the tree all its subtrees, corresponding to the children of vertex v, are of equal weight.
Count the minimum number of apples that you need to remove from the tree (specifically, from some of its leaves) in order to make the tree balanced. Notice that you can always achieve the goal by just removing all apples.
The first line contains integer n (2 ≤ n ≤ 105), showing the number of vertices in the tree. The next line contains n integersa1, a2, ..., an (0 ≤ ai ≤ 108), ai is the number of apples in the vertex number i. The number of apples in non-leaf vertices is guaranteed to be zero.
Then follow n - 1 lines, describing the tree edges. Each line contains a pair of integers xi, yi (1 ≤ xi, yi ≤ n, xi ≠ yi) — the vertices connected by an edge.
The vertices are indexed from 1 to n. Vertex 1 is the root.
Print a single integer — the minimum number of apples to remove in order to make the tree balanced.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the sin, cout streams cin, cout or the %I64d specifier.
60 0 12 13 5 61 21 31 42 52 6
6
题解:假设节点u的子节点的分支的最小公倍数是c[u],那么当u的子节点都平衡时,子节点的权值都会是c[u]的倍数,
如果任何一个子节点的权值不能变成c[u]的倍数时,就只能把整个树变成0
#include<cstdio>#include<algorithm>#include<string.h>#include<queue>#include<set>#include<functional>#include<iostream>#include<math.h>#include<vector>#include<string>#include<stdlib.h>using namespace std;typedef long long LL;const int maxn = 1e5 + 5;const LL inf = 1e18;struct Edge{ int v,nxt;}edge[maxn*2];LL d[maxn],c[maxn];int head[maxn],tot;LL gcd(LL a,LL b){return b==0?a:gcd(b,a%b);}LL lcm(LL a,LL b) {return a/gcd(a,b)*b;}void add(int u,int v){ edge[tot].v=v; edge[tot].nxt=head[u]; head[u]=tot++;}bool mark;void dfs(int u,int fa){ c[u]=1; LL m=inf,s=0; for(int i=head[u];~i;i=edge[i].nxt){ int v=edge[i].v; if(v==fa) continue; dfs(v,u); s++;//子节点个数 if(mark) return ; c[u]=lcm(c[u],c[v]);//最小公倍数表示如果父节点要减小,每个节点应同时减去多少 m=min(m,d[v]); if(m<c[u]) break; } if(s>0){ LL k=(m/c[u])*c[u];//如果能平衡,那么每个节点必然是最小公倍数的倍数 if(k==0){ mark=1;//如果有一个节点要变成0,那么整个树都要变成0 return; } d[u]=k*s; c[u]=c[u]*s;//要让每个子节点减小c[u],那么该节点就要减小c[u]*子节点个数 }}int main(){ int n; // freopen("in.txt","r",stdin); scanf("%d",&n); memset(head,-1,sizeof(head)); tot=0; LL sum=0; for(int i=1;i<=n;i++) {scanf("%I64d",&d[i]);sum+=d[i];} for(int i=0;i<n-1;i++){ int u,v; scanf("%d%d",&u,&v); add(u,v); add(v,u); } mark=0; dfs(1,-1); if(mark) printf("%I64d\n",sum); else printf("%I64d\n",sum-d[1]); return 0;}
- CodeForces-349D Apple Tree(树型dp)
- CodeForces 349D Apple Tree(树形DP)
- Codeforces 349D - Apple Tree
- Codeforces 462D Appleman and Tree【思维+树型Dp】
- POJ 2486 Apple Tree(树型DP)
- POJ 2486 Apple Tree ( 树型DP )
- 【POJ 2486】 Apple Tree(树型dp)
- Apple Tree--树形DP
- Apple Tree(树形DP)
- codeforces 161D D. Distance in Tree(树形dp)
- 【树型DP】codeforces 791d
- 树状DP POJ Apple Tree
- POJ2486:Apple Tree(树形DP)
- poj2486 Apple Tree 树形dp
- poj2486--Apple Tree(树状dp)
- 【poj2486】【Apple Tree】【树形dp】
- poj2486 Apple Tree 树形DP
- POJ2486 Apple Tree【树形dp】
- java远程调用中的RMI构建远程服务
- 获取汉字偏旁部首 Python版本
- Android性能优化第二弹
- 深入分析:Android中app之间的交互(二)
- TJU 1100 Pi double精度 gcd
- CodeForces-349D Apple Tree(树型dp)
- 如何提高自己的编程能力
- Volley框架学习
- 自定义全局异常捕获
- Linux简单命令
- 67 step to success
- java三大框架之spring整合hibernate4
- asp.net mvc 使用Ajax调用Action 返回数据。
- JCD国际拆分内排预热 QQ577614304