hdu6540 Neko and tree

 这是一道经典的树形dp,方法是分主子树进行dp。

坑点:主树的dp值会实时修改,所以不能用dp[ x ][ i ],只能用 temp1[ i ] - temp1[ i-1 ]

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef int lint;
const lint maxn= 5005;
const lint maxm = 10005;
const lint mod = 1e9 + 7;
lint n,m,k;
lint ver[maxm],ne[maxm],he[maxn],tot;
void add( lint x,lint y ){
    ver[++tot] = y;
    ne[tot] = he[x];
    he[x] = tot;
}
lint ad( lint x,lint y ){
    return ((LL)x+y)%mod;
}
lint mul( lint x,lint y ){
    return lint(1LL*x*y%mod);
}
lint dp[maxn][maxn], key[maxn];;
void dfs( lint x,lint f ){
    if( key[x] ) dp[x][0] = 1;
    for( lint cure = he[x];cure;cure = ne[cure] ){
        lint  y = ver[cure];
        if( y == f ) continue;
        dfs( y,x );
        static lint temp1[maxn];
        temp1[0] = dp[x][0];
        for( lint i = 1;i <= k;i++ ){
            temp1[i] = ad( dp[x][i],temp1[i-1] );
        }
        static lint temp2[maxn];
        temp2[0] = dp[y][0];
        for( lint i = 1;i <= k;i++ ){
            temp2[i] = ad( dp[y][i],temp2[i-1] );
        }

        for( lint i = 1;i <= k;i++ ){
            lint t = min( k-i,i );
            dp[x][i] = ad( dp[x][i], mul(dp[y][i-1] , temp1[t]+1) );
        }
        for( lint i = 1;i < k;i++ ){
            lint t = min( k-i,i-1 );
            if( t >= 1 )
            dp[x][i] = ad( dp[x][i],mul( (temp1[i]-temp1[i-1]+mod),temp2[t-1] ) );
        }
    }
    return;
}
int main(){
    tot = 1;
    scanf("%d%d%d",&n,&m,&k);
    for( lint x,y,i = 1;i <= n-1;i++ ){
        scanf("%d%d",&x,&y);
        add( x,y );add( y,x);
    }
    for( lint x,i = 1;i <= m;i++ ){
        scanf("%d",&x);
        key[x] =1;
    }
    dfs(1,0);
    lint ans=  0;
    for( lint i = 0;i <= k;i++ ){
        ans = ad( ans,dp[1][i] );
    }
    printf("%d\n",ans);
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值