Http://codeforces.com/contest/348/problem/B
B. Apple TreeTime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output
You is 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 any numbers in this subtree leaves. For instance, the weight of a subtree that corresponds to some leaf are the number written in the leaf.
A Tree is balanced if-every vertex v of the tree all its subtrees, corresponding to the children of V Ertex v, is of equal weight.
Count the minimum number of apples that's need to remove from the tree (specifically, from some of it leaves) in order To make the tree balanced. Notice that 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 integers a 1, a 2, ..., a n (0≤ a i ≤108), a i is the number of apples in the vertex number i . The number of apples in non-leaf vertices was guaranteed to be zero.
Then follow n -1 lines, describing the tree edges. Each line contains a pair of integers xi, yi (1≤ x i, y i ≤ n, xi ≠ yi )-the vertices connected by an edge.
The vertices is 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, don't 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.
Sample Test (s)
Input
6
0 0 12 13 5 6
1 2
1 3
1 4
2 5
2 6
Output
6
The main topic: there is a tree, each leaf node above a certain number of apples (not on the leaf node there is no Apple), now to make each node its sub-tree of the total number of apples are equal, ask at least how many apples to take away.
This problem is similar to last year's school PK race, but the problem is a binary tree, the scope is relatively small, so the direct violence enumeration of a leaf node value, and then a layer of the upward consideration on it can be.
And this question is not necessarily a two-fork tree, and the range is larger, then we can assume that the whole tree is the total number of apples X, we assume that the root node has n subtrees tree, then the number of apples to each subtrees tree is 1/n*x, and x must be a multiple of n (at the beginning is because this situation wa a few-_-| | | )。
Using recursion, the tree's apples are divided into sub-trees of the subtree ...
All the time to the leaf node, at this point we can know that the leaf node to the apple is a fraction of the X (set to K), so X is a multiple of k, and x/k is not much more than the original Apple number of the leaf, according to this, we can get a minimum of x, and then ask for an X of all k common multiple.
In addition, in order to avoid the multiplier when overflow, when found k>x, easy to know can only remove all the apples, direct output can.
1#include <iostream>2#include <stdio.h>3#include <stdlib.h>4#include <vector>5 using namespacestd;6 #defineMAXN 1000107typedefLong Longll;8 intN;9 intNUM[MAXN];Tenvector<int>G[MAXN]; Onell x,sum=0, lcm=1; A ll gcd (ll A,ll b) - { - returnb==0? A:GCD (b, a%b); the } - - voidDfsintCur=1, LL Div=1ll,intfa=-1) - { + if(div>x) - { +cout<<sum; AExit0); at return; - } - if(Num[cur] | | G[cur].size () <=1&& cur!=1)//It's a leaf node . - { -X=min (x, num[cur]*div); -lcm=div*lcm/gcd (lcm,div); in return; - } to for(intI=0; I<g[cur].size (); i++) + { - if(g[cur][i]!=FA) theDFS (G[cur][i], div* (LL) (G[cur].size ()-1* (cur!=1) ), cur); * } $ }Panax Notoginseng intMain () - { the //freopen ("In.txt", "R", stdin); + ACin>>N; the for(intI=1; i<=n; i++) + { -scanf"%d", &num[i]); $sum+=(LL) num[i]; $ } -x=sum; - for(intI=1; i<=n-1; i++) the { - intu,v;Wuyiscanf"%d%d", &u, &v); the G[u].push_back (v); - g[v].push_back (u); Wu } - dfs (); Aboutcout<<sum-x+x%LCM; $ - return 0; -}
Cf202-div 1-b-Apple tree: Search, number theory, tree traversal