zoukankan      html  css  js  c++  java
  • 【比赛】【树形dp】收集果子

    很容易就可以看出这是个树形dp

    但是怎么想都是n^3算法,60,卡常70

    附上主要dp过程:

    (1)直接填表

    然后去除了不合法的情况,

    在反复wa之后,我......A了???

    所以以后循环的范围要好好思考啊

    #include<cstdio>
    #include<cstdlib>
    #include<algorithm>
    #define ll long long
    using namespace std;
    int n; ll k;
    const int N=1003;
    const ll mod=1e9+7;
    int sz[N];
    
    ll val[N],tot_val[N],p[N];
    ll f[N][N];
    
    inline int read()
    {
        int x=0;char c=getchar();
        while(c<'0' || c>'9') c=getchar();
        while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-'0',c=getchar();
        return x;
    }
    struct node
    {int v,nx;}e[N<<1];
    int tot,head[N];
    void add(int u,int v)
    {
        e[++tot].v =v,e[tot].nx =head[u],head[u]=tot;
        e[++tot].v =u,e[tot].nx =head[v],head[v]=tot;
    }
    
    void dfs(int rt,int fa)
    {
        f[rt][val[rt]]=1;
        sz[rt]=1,tot_val[rt]=val[rt];
        
        for(int i=head[rt];i;i=e[i].nx )
        {
            int v=e[i].v ;
            if(v==fa) continue;
            dfs(v,rt);
            sz[rt]+=sz[v],tot_val[rt]+=tot_val[v];
            
            f[v][0]=(f[v][0]+p[sz[v]-1])%mod;
            for(ll j=min(k,tot_val[rt]);~j;j--)
            {
                f[rt][j]=f[rt][j]*f[v][0]%mod;
                for(ll kk=min(j,tot_val[v]) ; kk>=val[v] && kk>0 ;kk--)
                    f[rt][j]=( f[rt][j] + f[rt][j-kk]*f[v][kk]%mod )%mod;
            }
        }
    }
    
    int main()
    {
        n=read(),k=read();
        for(int i=1;i<=n;i++) scanf("%lld",&val[i]);
        for(int i=1;i<n;i++) 
            add(read(),read());
        p[0]=1;
        for(int i=1;i<=n;i++) p[i]=(p[i-1]<<1)%mod;
        
        dfs(1,0);
        printf("%d
    ",f[1][k]);
        
        return 0;
    }
    View Code
  • 相关阅读:
    假如
    Find the peace with yourself
    Sep 15th 2018
    Sep 10th 2018
    third party sales process 继续说
    成功设置open live writer
    sublime text2 基本配置及结合Python 环境
    Compmgmtlauncher.exe问题解决方法
    nginx 代理服务器
    vmware之linux不重启添加虚拟硬盘
  • 原文地址:https://www.cnblogs.com/xwww666666/p/11727113.html
Copyright © 2011-2022 走看看