cf202-div 1-B - Apple Tree:搜索,数论,树的遍历
2 seconds
256 megabytes
standard input
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.
The first line contains integer n (2 ≤ n ≤ 105), showing the number of vertices in the tree. The next line contains n integers a1, 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.
6
0 0 12 13 5 6
1 2
1 3
1 4
2 5
2 6
6
题目大意:有一棵有根树,每个叶子节点上面有一定数量的苹果(非叶子节点上没有苹果),现在要使每一个节点它的各个子树上的苹果总数都相等,问至少需要拿走多少个苹果。 这题跟去年校内PK赛的一道暴力题很像,不过那题是一棵二叉树,范围又比较小,所以直接暴力枚举某叶子节点的值,再一层层的往上考虑就可以了。 而这题不一定是二叉树,而且范围比较大,这时我们可以假设整棵树的苹果总数为x,我们假设根节点有n棵子树,那么分给每棵子树的苹果数为1/n*x,并且x一定是n的倍数(刚开始就是因为没考虑到这种情况WA了几发-_-|||)。 利用递归,将子树的苹果平均分给子树的子树。。。 一直到叶子节点,此时我们可以知道,该叶子节点分到的苹果为x的几分之一(设为k),因此x为k的倍数,并且x/k不大于该叶子的原来的苹果数,根据这个,我们可以得到一个最小的x,最后再求一个不大于x的所有k的公倍数,得解。 另外,为了避免倍数累乘的时候溢出,当发现k>x时,易知只能将所有的苹果都移掉,直接输出即可。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
#define MAXN 100010
typedef long long ll;
int n;
int num[MAXN];
vector<int> G[MAXN];
ll x,sum=,lcm=;
ll gcd(ll a,ll b)
{
return b== ? a : gcd(b, a%b);
} void dfs(int cur=, ll div=1LL, int fa=-)
{
if(div>x)
{
cout<<sum;
exit();
return;
}
if(num[cur] || G[cur].size()<= && cur!=) // 是叶子节点
{
x=min(x, num[cur]*div);
lcm=div*lcm/gcd(lcm,div);
return;
}
for(int i=; i<G[cur].size(); i++)
{
if(G[cur][i]!=fa)
dfs(G[cur][i], div*(ll)(G[cur].size()-*(cur!=)),cur);
}
}
int main()
{
//freopen("in.txt","r", stdin); cin>>n;
for(int i=; i<=n; i++)
{
scanf("%d", &num[i]);
sum+=(ll)num[i];
}
x=sum;
for(int i=; i<=n-; i++)
{
int u,v;
scanf("%d %d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs();
cout<<sum-x+x%lcm; return ;
}
cf202-div 1-B - Apple Tree:搜索,数论,树的遍历的更多相关文章
- [poj3321]Apple Tree(dfs序+树状数组)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26762 Accepted: 7947 Descr ...
- POJ 题目3321 Apple Tree(线段树)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21566 Accepted: 6548 Descr ...
- Codeforces Round #329 (Div. 2) D. Happy Tree Party LCA/树链剖分
D. Happy Tree Party Bogdan has a birthday today and mom gave him a tree consisting of n vertecie ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- POJ 3321 Apple Tree DFS序 + 树状数组
多次修改一棵树节点的值,或者询问当前这个节点的子树所有节点权值总和. 首先预处理出DFS序L[i]和R[i] 把问题转化为区间查询总和问题.单点修改,区间查询,树状数组即可. 注意修改的时候也要按照d ...
- 1020. Tree Traversals (25) ——树的遍历
//题目 通过后续遍历 中序遍历 得出一棵树 ,然后按树的层次遍历打印 PS:以前对于这种用指针的题目是比较头痛的,现在做了一些链表操作后,感觉也不难 先通过后续中序建一棵树,然后通过BFS遍历这棵树 ...
- ACM学习历程——POJ3321 Apple Tree(搜索,线段树)
Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will ...
- POJ3321 Apple Tree (树状数组)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16180 Accepted: 4836 Descr ...
随机推荐
- Apache调优
1.调整文件描述符大小,默认Linux系统的文件描述符是1024,对于squid等一些服务来说 在高负载的情况下这些文件描述符是远远不够的,所以在部署该类服务器时修改文件 ...
- css动画+滚动的+飞舞的小球
源代码如下: <!DOCTYPE html><html><head> <title>xi</title> <meta charset= ...
- hdu 2218
题意: 切蛋糕问题 水题...... AC代码: #include <iostream> using namespace std; int main() { int a[6],i,n; c ...
- java的@see注释
@see注释用法 @see 类名 @see #方法名或属性名 @see 类名#方法名或属性名
- 请帮我看看这个页面,红色部份如何改才能保存到ACCess数据库中
<% if session("shiwei_username")="" then %> <script language="java ...
- 开发中遇到的java小知识
今天在优化一个功能的时候遇到了instr与substr函数,之前没有接触过这两个函数,但是今天无意中用到,一查才发现,真是实用的一对兄弟啊. 先来说说substr函数,这个函数返回的是字符串的一部分. ...
- XML DOM 总结一
对这个基本概念我不介绍太多,无非就是一定格式的文本而已,我现在侧重于如何使用它. 首先看看.NET对它的支持. 首先看看这个类图: 所有的都是基于Xm ...
- oracle中存储过程详解
oracle中存储过程的使用 过程是指用于执行特定操作的PL/SQL块.如果客户应用经常需要执行特定操作,那么可以考虑基于这些操作建立过程.通过使用过程,不仅可以简化客户应用的开发和维护,而且可以提高 ...
- tomcat加入系统服务
在实际的项目开发中web容器等都是安装在客户方的服务器上的,在实现tomcat的集群时为了实现防止客户重启的机器造成服务器的关闭因此需要将web容器加入到系统服务中,在系统启动的时候自动启动服务,以t ...
- thinkphp M 和模板用法
<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller { pu ...