1097G Vladislav and a Great Legend

传送门

分析

https://blog.csdn.net/forever_shi/article/details/88048528

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define int long long
const int mod = 1e9+7;
int dp[100100][210],now[210],siz[100100],n,k;
int s[210][210],p[210],res[210];
vector<int>v[100100];
inline void init(){
    int i,j;
    p[0]=1;
    for(i=1;i<=k;i++)p[i]=p[i-1]*i%mod;
    s[1][1]=1;
    for(i=2;i<=k;i++)
      for(j=1;j<=i;j++)
        s[i][j]=(s[i-1][j-1]+s[i-1][j]*j%mod)%mod;
}
inline void dfs(int x,int fa){
    dp[x][0]=1;
    for(int i=0;i<v[x].size();i++){
      if(v[x][i]==fa)continue;
      dfs(v[x][i],x);
      for(int j=0;j<=k;j++)now[j]=dp[x][j];
      for(int j=1;j<=k;j++)dp[x][j]=(dp[x][j]+dp[v[x][i]][j]+dp[v[x][i]][j-1])%mod;
      dp[x][0]=(dp[x][0]+dp[v[x][i]][0])%mod;
      for(int j=0;j<=min(k,siz[x]);j++)
        for(int t=0;t<=min(k-j,siz[v[x][i]]);t++){
          dp[x][j+t]=(dp[x][j+t]+now[j]*dp[v[x][i]][t]%mod)%mod;
          dp[x][j+t+1]=(dp[x][j+t+1]+now[j]*dp[v[x][i]][t]%mod)%mod;
          res[j+t]=(res[j+t]+now[j]*dp[v[x][i]][t]%mod)%mod;
          res[j+t+1]=(res[j+t+1]+now[j]*dp[v[x][i]][t]%mod)%mod;
        }
      siz[x]+=siz[v[x][i]]+1;
    } 
}
signed main(){
    int i,j,Ans=0;
    scanf("%lld%lld",&n,&k);
    for(i=1;i<n;i++){
      int x,y;
      scanf("%lld%lld",&x,&y);
      v[x].push_back(y);
      v[y].push_back(x);
    }
    init();
    dfs(1,0);
    for(i=1;i<=k;i++)
      Ans=(Ans+p[i]*s[k][i]%mod*res[i]%mod)%mod;
      cout<<Ans;
    return 0;
}
posted @ 2019-03-25 10:38  水题收割者  阅读(167)  评论(0编辑  收藏  举报