codeforces348b Apple Tree DFS

Source: Internet
Author: User

Test instructions: The number of apples per leaf on an apple tree is different, and it is now necessary to remove the fewest apples from the apple tree, so that for each node, all of his subtrees have the same number of apples.

two times Dfs.

For two sub-trees, if the first subtree has four nodes, the second subtrees tree has five nodes, and because the weight of the first subtrees tree equals the weight of the second subtrees tree, the weights of the two trees must be multiples of 4 and 5 multiples, so each subtrees tree is a multiple of 20. To understand this, we can get the conclusion that for the first tree, it translates into 20 brothers, and the second tree can be turned into 20 siblings, so the father of the two trees can turn into 40 nodes side by side. Then, to think about the four sons of the first tree, it is clear that each son must be a multiple of 5, so each son can become five copies, which is equivalent to the first node becoming 20 parts.

Now solve this subtrees tree can be divided into how many parts, the following to solve, each part of the maximum is how much, obviously, for each leaf node L = min (l, leaf node weight v/leaf node should be divided into num parts)

1#include <iostream>2#include <vector>3#include <cstdio>4#include <cstdlib>5 using namespacestd;6 Long Longa[1111111];7 Long Longs[1111111];8 Long LongS;9 Long LongL =1e15;Ten Long LongAns =0; One     intN; Avector<int> e[1111111]; - Long LonggcdLong LongXLong Longy) - { the     returny==0? X:GCD (y,x%y); - } - Long LongLcaLong LongXLong  Longy) - { +     returnx* (y/gcd (x, y)); - } + Long LongDfsintUintp) A { at     if(e[u].size () = =1&&u!=1) -     { -S[u] =A[u]; -         return1LL; -     } -     Long Longnum =1; in      for(inti =0; I<e[u].size (); i++) -     { to         if(e[u][i]==p)Continue; +num =LCA (Num,dfs (E[u][i],u)); -S[u] + =S[e[u][i]]; the     } *     if((E[u].size ()-(u==1?0:1)) *num >= s) {cout << s <<endl;exit (0);} $     return(E[u].size ()-(u==1?0:1))*num;Panax Notoginseng } - voidDFS2 (intUintPLong Longnum) the { +     if(e[u].size () = =1&&u!=1) A     { theL = min (l,s[u]/num); +     } -      for(inti =0; I<e[u].size (); i++) $     { $         if(e[u][i]==p)Continue; -DFS2 (e[u][i],u,num/(E[u].size ()-(u==1?0:1))); -     } the } - intMain ()Wuyi { the  -     //freopen ("In.txt", "R", stdin); WuCIN >>N; -      for(inti =1; i<=n; i++) About     { $scanf"%i64d",&a[i]); -s+=A[i]; -     } -      for(inti =1; I<n; i++) A     { +         intx, y; thescanf"%d%d",&x,&y); - e[x].push_back (y); $ e[y].push_back (x); the     } the     Long LongDD = DFS (1,0); theDFS2 (1,0, DD); the     if(DD * L >=S) -     { incout <<0<<Endl; the     } the     Else About     { thecout << s-dd*l<<Endl; the     } the     return 0; +}
View Code

codeforces348b Apple Tree DFS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.