NC 蓝魔法师

链接:https://ac.nowcoder.com/acm/problem/20811
来源:牛客网

题目描述
“你,你认错人了。我真的,真的不是食人魔。”–蓝魔法师

给出一棵树,求有多少种删边方案,使得删后的图每个连通块大小小于等于k,两种方案不同当且仅当存在一条边在一个方案中被删除,而在另一个方案中未被删除,答案对998244353取模
输入描述:
第一行两个整数n,k, 表示点数和限制
2 <= n <= 2000, 1 <= k <= 2000
接下来n-1行,每行包括两个整数u,v,表示u,v两点之间有一条无向边
保证初始图联通且合法
输出描述:
共一行,一个整数表示方案数对998244353取模的结果
示例1
输入
复制
5 2
1 2
1 3
2 4
2 5
输出
复制
7

#include<unordered_set>
#include<unordered_map>
#include<functional>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<set>
#include<map>

using namespace std;
//================================
#define debug(a) cout << #a": " << a << endl;
#define rep(i, ll,rr) for(int i = ll; i <= rr; ++i)
const int mod = 998244353;
//================================
typedef pair<int,int> pii;
#define x first
#define y second
typedef long long LL; typedef unsigned long long ULL; typedef long double LD;
inline LL read() { LL s = 0, w = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') w = -1; for (; isdigit(ch); ch = getchar())    s = (s << 1) + (s << 3) + (ch ^ 48); return s * w; }
inline void print(LL x, int op = 10) { if (!x) { putchar('0'); if (op)  putchar(op); return; }  char F[40]; LL tmp = x > 0 ? x : -x; if (x < 0)putchar('-');  int cnt = 0;    while (tmp > 0) { F[cnt++] = tmp % 10 + '0';     tmp /= 10; }    while (cnt > 0)putchar(F[--cnt]);    if (op) putchar(op); }
//================================= 
/*
i+j<=m:
    f[u][i+j] = (f[u][i+j]+f[son1][i]*f[son2][j])%mod;
不与当前结点相连:
    f[u][j] = f[u][j]*f[son] 
*/

const int N = 2010;
int e[2*N],ne[2*N],h[N],idx=0;
int f[N][N],n,m,sz[N];  //f[u][i]表示以u为根所在连通块的数量为i的方案数

void add(int a,int b){
    e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}

void dfs(int u,int pre){
    f[u][1] = sz[u] = 1;
    for(int i=h[u];~i;i=ne[i]){
        int j=e[i];
        if(j==pre) continue;
        dfs(j,u);
        
        LL tmp=0; //记录儿子节点的方案
        for(int s=0;s<=min(sz[j],m);s++)
            (tmp+=f[j][s])%=mod;
        
        for(int fst=min(m,sz[u]);fst>=1;fst--){
            for(int sed=min(m,sz[j]);sed>=1;sed--){
                if(fst+sed>m) continue;
                //由乘法原理
                f[u][fst+sed] = ((LL)f[u][fst+sed] + (LL)f[u][fst]*f[j][sed])%mod; 
            }
            f[u][fst] = (LL)f[u][fst]*tmp%mod;
        }
        sz[u]+=sz[j];
    }
}


//=================================
int main(signed argc,char const *argv[])
{
    clock_t c1 = clock();
#ifdef LOCAL
    freopen("in.in", "r", stdin);
    freopen("out.out", "w", stdout);    
#endif 
    //=======================================
    memset(h,-1,sizeof h);
    scanf("%d%d",&n,&m);
    for(int i=1;i<n;i++){
        int a,b;
        scanf("%d%d",&a,&b);
        add(a,b),add(b,a);
    }
    dfs(1,-1);
    LL ans=0;
    for(int i=1;i<=m;i++)
        ans=(ans+f[1][i])%mod;
    print(ans);
    //=======================================
end:
    cerr << "Time Used:" << clock() - c1 << "ms" << endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值