• I


    I - Apple Tree
    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
    Submit Status
    Appoint description: 

    Description

    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.

    Sample Input

    Input
    6
    0 0 12 13 5 6
    1 2
    1 3
    1 4
    2 5
    2 6
    Output
    6
     
    const LL INF = 100000000000000;
    const double eps = 1e-8;
    const int maxn = 300100;
    LL v[maxn];
    vector<int> g[maxn];
    vector<int> G[maxn];
    LL dp[maxn];
    LL ans;
    LL leaf[maxn];
    LL gcd(LL x,LL y)
    {
        return y == 0 ? x : gcd(y,x%y);
    }
    LL lcm(LL x,LL y)
    {
        return x/gcd(x,y)*y;
    }
    bool flag;
    LL dfs(int x)
    {
        if(flag == false) return -1;//1
        if(dp[x] != -1) return  dp[x];
        LL sum = 0;
        LL LCM;
        if(g[x].size() == 0)//2
            LCM = 1;
        else
            LCM = leaf[x]/g[x].size();
        LL Min = INF;
        rep(i,0,g[x].size())
        {
            if(g[x][i] == x) continue;
            Min = min(Min,dfs(g[x][i]));
        }
        rep(i,0,g[x].size())
        {
            if(g[x][i] == x) continue;
            sum += dp[g[x][i]];
        }
        if(Min < LCM) {//3
            flag = false;
            return -1;
        }
        Min = Min - Min%LCM;
         
        dp[x] = g[x].size()*Min;
        ans += sum - dp[x];
        if(dp[x] < leaf[x])//4
        {
            flag = false;
            return -1;
        }
        return dp[x];
    }
    bool vis[maxn];
    void dfs1(int x)
    {
        rep(i,0,G[x].size())
        {
            if(!vis[G[x][i]])
            {
                g[x].push_back(G[x][i]);
                vis[G[x][i]] = true;
                dfs1(G[x][i]);
            }
        }
    }
    
    LL dfs2(int x)
    {
        if(leaf[x]) return leaf[x];
        if(g[x].size() == 0)
        {
            leaf[x] = 1;
            return 1;
        }
        
        LL LCM = 1;
        rep(i,0,g[x].size())
        {
            if(g[x][i] == x) continue;
            LL T = dfs2(g[x][i]);
            LCM = lcm(LCM,T);
        }
        leaf[x] = LCM*g[x].size();
        return leaf[x];
    }
    int main() 
    {
        //freopen("in.txt","r",stdin);
        int n;
        while(cin>>n)
        {
            clr(leaf);
            repf(i,1,n) dp[i] = -1;
            LL SUM = 0;
            repf(i,1,n) 
            {
                scanf("%I64d",&v[i]);
                SUM += v[i];
                if(v[i]) dp[i] = v[i],leaf[i] = 1;
            }
    
            repf(i,1,n-1)
            {
                int a,b;
                scanf("%d%d",&a,&b);
                G[a].push_back(b);
                G[b].push_back(a);
            }
            ans = 0;
            clr(vis);
            vis[1] = 1;
            flag = true;
            dfs1(1);
            dfs2(1);
            dfs(1);
            if(flag == false)
                cout<<SUM<<endl;
            else
                cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    【转】How Google Glass Actually Works
    【转】苹果、微软、Google的下一战:未来智能眼镜
    【转】硅谷创业教父Paul Graham:如何获得创业idea (1)
    【转】解读专利文件:下一代Google Glass将更像“正常”眼镜,通过镜片显示画面
    【转】苹果只是现在,Google才是未来
    【Coding】程序员每天该做的事 ,收藏着
    【Coding】Enterprise Architect 7.0入门教程
    【Linux】Ubuntu下Java编程环境的搭建
    WebPart开发知识 (转)
    用于 SharePoint 开发的 Visual Studio 2010 工具 (转)
  • 原文地址:https://www.cnblogs.com/DreamHighWithMe/p/3450288.html
Copyright © 2020-2023  润新知