zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Cg:访问OpenGL的状态
    C++ Exception Handling
    C语言的调用规约(Calling Convension)之参数传递和返回值
    Why is FBX readonly in animation editor when imported?
    如何在Visual Studio中编译wxWidgets
    ICU字符集编码转换一例
    VisTools
    关于数值分析和LCP问题的一些开源项目
    C++: The Case Against Global Variables
    老男孩筷子兄弟
  • 原文地址:https://www.cnblogs.com/DreamHighWithMe/p/3450288.html
Copyright © 2011-2022 走看看