CodeForces-349D Apple Tree(树型dp)

来源:互联网 发布:onese刻录dvd数据 编辑:程序博客网 时间:2024/05/01 18:18

D. Apple Tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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.

Output

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 cincout or the %I64d specifier.

Examples
input
60 0 12 13 5 61 21 31 42 52 6
output
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;}



0 0
原创粉丝点击