51nod 1353 树

题目
题解
不太懂为什么是 O ( n 2 ) 的,有知道的麻烦解释一下,谢谢

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M=1e9+7,N=2002;
struct node{
    int to,ne;
}e[N<<1];
int tot,h[N],f[N][N],s[N],i,n,k,x,y;
void add(int x,int y){
    e[++tot]=(node){y,h[x]};
    h[x]=tot;
}
void dfs(int u,int fa){
    f[u][s[u]=1]=1;
    for (int t=h[u],v;t;t=e[t].ne)
        if ((v=e[t].to)!=fa){
            dfs(v,u);
            for (int i=s[u];i;i--){
                for (int j=s[v];j;j--) (f[u][i+j]+=(ll)f[u][i]*f[v][j]%M)%=M;
                f[u][i]=(ll)f[u][i]*f[v][0]%M;
            }
            s[u]+=s[v];
        }
    for (int i=k;i<=s[u];i++) (f[u][0]+=f[u][i])%=M;
}
int main(){
    scanf("%d%d",&n,&k);
    for (i=1;i<n;i++) scanf("%d%d",&x,&y),add(x,y),add(y,x);
    dfs(1,0);
    printf("%d",f[1][0]);
}

猜你喜欢

转载自blog.csdn.net/xumingyang0/article/details/80862698
今日推荐