B. Apple Tree violence + Math

Source: Internet
Author: User

Http://codeforces.com/problemset/problem/348/B

Notice that if the value of the vertex is determined, then the number of its points is determined, so the value of the vertex can be violently enumerated.

The number of vertices is separated from the LCM, LCM is, for example, 1 has three sub-nodes, then 1 of the value is at least a multiple of 3, or not divisible.

Similarly, 1 have three sons 2, 3, 4, if 3 have three sons, then 1 will be at least a multiple of 9, because the need to divide to 3 is at least 3.

So the LCM of the whole tree is calculated, the leaf node LCM is 1, the other LCM = LCM (All child nodes) * Son[cur]

After this, you can force the enumeration of the value of vertex 1, each enumeration is DFS once, a little violent 1700ms, learning to be solved first.

#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#include<assert.h>#defineIOS Ios::sync_with_stdio (False)using namespacestd;#defineINF (0X3F3F3F3F)typedefLong Long intLL; #include<iostream>#include<sstream>#include<vector>#include<Set>#include<map>#include<queue>#include<string>Const intMAXN = 1e5 + -;intA[MAXN]; LL sum;structNode {intu, V, tonext;} E[MAXN*2];intFIRST[MAXN], num;voidAddedge (intUintv) {++num; E[NUM].U= u, e[num].v = V, E[num].tonext =First[u]; First[u]=num;}intVIS[MAXN], DFN =1;intSON[MAXN];voidFindson (intcur) {     for(inti = first[cur]; I i =E[i].tonext) {        intv =e[i].v; if(Vis[v] = = DFN)Continue; VIS[V]=DFN; Son[cur]++;    Findson (v); }}ll ans=1e18l, Tans; LL LCM; ll LCM (ll A, ll b) {returnA/__GCD (A, b) *b;}BOOLFlag;voidDfsintcur, LL val) {    if(flag)return; if(Son[cur] = =0) {        if(Val >A[cur]) {Flag=true; return; } Tans+ = A[cur]-Val; return; }     for(inti = first[cur]; I i =E[i].tonext) {        intv =e[i].v; if(Vis[v] = = DFN)Continue; VIS[V]=DFN; if(val% son[cur]! =0) {flag=true; return; } dfs (V, Val/Son[cur]); }}ll Calc (intcur) {    if(Son[cur] = =0)return 1; LL THISLCM=1;  for(inti = first[cur]; I i =E[i].tonext) {        intv =e[i].v; if(Vis[v] = = DFN)Continue; VIS[V]=DFN; THISLCM=LCM (THISLCM, Calc (v)); }    if(THISLCM > Sum/Son[cur]) {printf ("%i64d\n", sum); Exit (0); }    returnSon[cur] *THISLCM;}voidWork () {intN; scanf ("%d", &N); LCM=1;  for(inti =1; I <= N; ++i) {scanf ("%d", &A[i]); Sum+=A[i]; }     for(inti =1; I <= N-1; ++i) {intu, v; scanf ("%d%d", &u, &v);        Addedge (U, v);    Addedge (V, u); } vis[1] =DFN; Findson (1); ++DFN; vis[1] =DFN; LCM= Calc (1);//cout << LCM << Endl;Ans =sum; Sum/=LCM; Sum*=LCM;  for(LL i = sum; I >= LCM; I-=LCM) {++DFN, tans =0; Flag=false;//DFS (1);vis[1] =DFN; DFS (1, i); if(flag)Continue; printf ("%i64d\n", ans-i); return; } printf ("%i64d\n", ans);}intMain () {#ifdef local freopen ("Data.txt","R", stdin);//freopen ("Data.txt", "w", stdout);#endifWork (); return 0;}
View Code

B. Apple Tree violence + Math

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.