P4516 [JSOI2018] 潜入行动

\(f_{i,j,0/1,0/1}\) 表示以 \(i\) 为根的子树中,选了 \(j\) 个点,\(i\) 有/没有被选择, \(i\) 有/没有被覆盖的方案数。
转移:

\[\begin{array}{lr} f'_{u,i+j,0,0}=\sum_{v\in son(u)} f_{u,i,0,0}\times f_{v,j,0,1}\\ f'_{u,i+j,0,1}=\sum_{v\in son(u)} f_{u,i,0,1}\times(f_{v,j,1,1}+f_{v,j,0,1})+f_{u,i,0,0}\times f_{v,j,1,1}\\ f'_{u,i+j,1,0}=\sum_{v\in son(u)} f_{u,i,1,0}\times(f_{v,j,0,1}+f_{v,j,0,0})\\ f'_{u,i+j,1,1}=\sum_{v\in son(u)} f'_{u,i,1,1}\times(f_{v,j,0,0}+f_{v,j,1,0}+f_{v,j,0,1}+f_{v,j,1,1})+f_{u,i,1,0}\times(f_{v,j,1,0}+f_{v,j,1,1}) \end{array} \]

然后跑一个树上背包,时间复杂度 \(\mathcal O(nk)\)

代码

/*
Luogu P4516 [JSOI2018] 潜入行动
2026-03-30
*/
#include<bits/stdc++.h>
using namespace std;
namespace IO{
    template<typename T>
    inline void read(T&x){
        x=0;char c=getchar();bool f=0;
        while(!isdigit(c)) c=='-'?f=1:0,c=getchar();
        while(isdigit(c)) x=x*10+c-'0',c=getchar();
        f?x=-x:0;
    }
    template<typename T>
    inline void write(T x){
        if(x==0){putchar('0');return ;}
        x<0?x=-x,putchar('-'):0;short st[50],top=0;
        while(x) st[++top]=x%10,x/=10;
        while(top) putchar(st[top--]+'0');
    }
    inline void read(char&c){c=getchar();while(isspace(c)) c=getchar();}
    inline void write(char c){putchar(c);}
    inline void read(string&s){s.clear();char c;read(c);while(!isspace(c)&&~c) s+=c,c=getchar();}
    inline void write(string s){for(int i=0,len=s.size();i<len;i++) putchar(s[i]);}
    template<typename T>inline void write(T*x){while(*x) putchar(*(x++));}
    template<typename T,typename...T2> inline void read(T&x,T2&...y){read(x),read(y...);}
    template<typename T,typename...T2> inline void write(const T x,const T2...y){write(x),putchar(' '),write(y...),sizeof...(y)==1?putchar('\n'):0;}
}using namespace IO;
template<int mod>struct Modint{
    int z;
    Modint(){z=0;}
    Modint(int x){x%=mod;z=x<0?x+mod:x;}
    Modint(long long x){x%=mod;z=x<0?x+mod:x;}
    Modint(short x){x%=mod;z=x<0?x+mod:x;}
    Modint(char x){x%=mod;z=x<0?x+mod:x;}
    Modint(bool x){x%=mod;z=x<0?x+mod:x;}
    friend Modint operator+(Modint t,Modint t2){Modint ans;ans.z=(t.z+t2.z)%mod;return ans;}
    friend Modint operator*(Modint t,Modint t2){Modint ans;ans.z=1ll*t.z*t2.z%mod;return ans;}
    friend Modint operator-(Modint t,Modint t2){Modint ans;ans.z=(t.z-t2.z)%mod;return ans;}
    Modint operator<<(const int t)const{Modint ans;ans.z=(z<<t)%mod;return ans;}
    Modint operator>>(const int t)const{Modint ans;ans.z=(z>>t)%mod;return ans;}
    Modint&operator+=(const Modint t){z=(z+t.z)%mod;return *this;}
    Modint&operator*=(const Modint t){z=1ll*z*t.z%mod;return *this;}
    Modint&operator-=(const Modint t){z=(z-t.z)%mod;return *this;}
    Modint&operator<<=(const int t){z=(z<<t)%mod;return *this;}
    Modint&operator>>=(const int t){z=(z>>t)%mod;return *this;}
    friend Modint ksm(Modint a,int b){
        Modint ans=1;
        while(b){if(b&1) ans=ans*a;a=a*a,b>>=1;}
        return ans;
    }
    friend void read(Modint&z){
        int x=0;char c=getchar();bool f=0;
        while(!isdigit(c)) c=='-'?f=1:0,c=getchar();
        while(isdigit(c)) x=(x*10ll+c-'0')%mod,c=getchar();
        f?x=-x:0;
        z.z=x;
    }
    friend void write(Modint x){x.z<0?x.z+=mod:0;write(x.z);}
};
const int mod=1000000007,maxn=100010,maxk=110;
#define M Modint<mod>
int n,k,sz[maxn];
vector<int>e[maxn];
M f[maxn][maxk][2][2],nw[maxk][2][2];
void dfs(int u,int fa=0){
    sz[u]=1;
    for(int v:e[u]){
        if(v==fa) continue;
        dfs(v,u);
    }
    f[u][0][0][0]=f[u][1][1][0]=1;
    for(int v:e[u]){
        if(v==fa) continue;
        for(int i=0,_=min(sz[u],k);i<=_;i++) for(int j=0;j<=sz[v];j++){
            if(i+j>k) break;
            nw[i+j][0][0]+=f[u][i][0][0]*f[v][j][0][1];
            nw[i+j][0][1]+=f[u][i][0][1]*(f[v][j][1][1]+f[v][j][0][1])+f[u][i][0][0]*f[v][j][1][1];
            nw[i+j][1][0]+=f[u][i][1][0]*(f[v][j][0][1]+f[v][j][0][0]);
            nw[i+j][1][1]+=f[u][i][1][1]*(f[v][j][0][0]+f[v][j][0][1]+f[v][j][1][0]+f[v][j][1][1])+f[u][i][1][0]*(f[v][j][1][0]+f[v][j][1][1]);
        }
        sz[u]+=sz[v];
        for(int i=0,_=min(k,sz[u]);i<=_;i++) for(int x=0;x<=1;x++) for(int y=0;y<=1;y++) f[u][i][x][y]=nw[i][x][y],nw[i][x][y]=0;
    }
}
signed main(){
    read(n,k);
    for(int i=1;i<n;i++){
        int u,v;read(u,v);
        e[u].push_back(v),e[v].push_back(u);
    }
    dfs(1);
    write(f[1][k][0][1]+f[1][k][1][1]);
    return 0;
}
posted @ 2026-03-30 19:25  Link-Cut_Trees  阅读(3)  评论(0)    收藏  举报