• bzoj3727: PA2014 Final Zadanie


    我真是SB之神呢这么SB的题都不会

    肯定是先无脑正向思考,罗列下关系式:

    b[1]=∑a[i]*dep[i]=∑tot[i] (i!=1)

    b[i]=b[fa]-tot[i]+(tot[1]-tot[i])

    a[i]=tot[i]-∑tot[son]

    画一下第二个,就变成2*tot[i]-tot[1]=b[fa]-b[i],那么i取遍2~n就有 2*∑tot[i](i!=1) - (n-1)*tot[1] = ∑b[fa]-b[i]

    后面那个是可以算的,∑tot[i](i!=1)就是b1,那tot[1]就搞出来了

    然后把tot[1]代入第二个柿子,tot就全搞出来了

    然后再代入第三个,a也完了

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    typedef long long LL;
    const int _=1e2;
    const int maxn=3*1e5+_;
    
    struct node
    {
        int x,y,next;
    }a[2*maxn];int len,last[maxn];
    void ins(int x,int y)
    {
        len++;
        a[len].x=x;a[len].y=y;
        a[len].next=last[x];last[x]=len;
    }
    int fa[maxn];
    void dfs(int x)
    {
        for(int k=last[x];k;k=a[k].next)
        {
            int y=a[k].y;
            if(fa[x]!=y)
            {
                fa[y]=x;
                dfs(y);
            }
        }
    }
    LL A[maxn],B[maxn],tot[maxn];
    void dfs2(int x)
    {
        A[x]=tot[x];
        for(int k=last[x];k;k=a[k].next)
        {
            int y=a[k].y;
            if(fa[x]!=y)
            {
                A[x]-=tot[y];
                dfs2(y);
            }
        }
    }
    
    int main()
    {
        int n,x,y;
        scanf("%d",&n); len=1;
        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            ins(x,y),ins(y,x);
        }
        for(int i=1;i<=n;i++)scanf("%lld",&B[i]);
        dfs(1);
        
        LL s=0;
        for(int i=2;i<=n;i++)s+=B[fa[i]]-B[i];
        tot[1]=(2*B[1]-s)/LL(n-1);
        for(int i=2;i<=n;i++)
            tot[i]=(B[fa[i]]-B[i]+tot[1])/2;
        dfs2(1);
        for(int i=1;i<n;i++)printf("%lld ",A[i]);
        printf("%lld
    ",A[n]);
        
        return 0;
    }
  • 相关阅读:
    Disharmony Trees HDU
    Xenia and Bit Operations CodeForces
    Gym
    背包入门
    搜索入门
    Farm Tour POJ
    Flow Problem HDU
    Hie with the Pie POJ
    Building a Space Station POJ
    kuangbin 最短路集合
  • 原文地址:https://www.cnblogs.com/AKCqhzdy/p/10412891.html
Copyright © 2020-2023  润新知