1 solutions

  • 0
    @ 2025-10-3 15:36:13

    独立集(set)

    首先,有一个显然的 O(nm)O(n m) 的 DP。 考虑如何扔掉一个 m,考虑每次只有单点加,于是我们按顺序加入,那么状压一下到根路径上的点是否被选就好了,可以通过满二叉树。 考虑优化,这里有两种做法。

    法一:

    考虑到这个过程中,为啥我们需要记录到根的路径上所有的点的状态。即加入的下一个点是当前的祖先的另一个儿子。而不是这种情况的我们只需要再多记录一个当前点的状态就好了,所以考虑树链剖分,每次优先遍历轻儿子,这样,路径上我们记录的点就只有重链链头了,复杂度 O(n2m)O(n^{2} m)。

    法二:

    尝试直接建出一棵高度为的树替代原树。考虑点分树。注意到任意在原树上的边在点分树上连接的都是祖先到后代,于是我们直接按照完全二叉树的做法做即可,复杂度 O(n2m)O(n^{2} m),更为好写,采用这种方法。

    #include<bits/stdc++.h>
    using namespace std;
    namespace IO{
        char buff[1<<21],*p1=buff,*p2=buff;
        inline char getch(){
            return p1==p2&&(p2=((p1=buff)+fread(buff,1,1<<21,stdin)),p1==p2)?EOF:*p1++;
        }
        template<typename T>
        inline void read(T &x){
            char ch=getch();int fl=1;x=0;
            while(ch>'9'||ch<'0'){if(ch=='-')fl=-1;ch=getch();}
            while(ch<='9'&&ch>='0'){x=x*10+ch-48;ch=getch();}
            x*=fl;
        }
        template<typename T,typename ...Args>
        inline void read(T &x,Args& ...args){
            read(x);read(args...);
        }
        char obuf[1<<21],*p3=obuf;
        inline void putch(char ch){
            if(p3-obuf<(1<<21))*p3++=ch;
            else fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=ch;
        }
        char ch[100];
        template<typename T>
        void write(T x){
            if(!x)return putch('0');
            if(x<0)putch('-'),x*=-1;
            int top=0;
            while(x)ch[++top]=x%10+48,x/=10;
            while(top)putch(ch[top]),top--;
        }
        template<typename T,typename ...Args>
        void write(T x,Args ...args){
            write(x);write(args...);
        }
        void flush(){fwrite(obuf,p3-obuf,1,stdout);}
    }
    using namespace IO;
    const int N=105,M=1e4+5,mod=998244353;
    inline int Mod(int x){return x>=mod?x-mod:x;}
    struct node{
    	int x,y;
    	friend node operator +(node A,int B){
    		return node{A.x+B,A.y};
    	}
    	friend node operator +(node A,node B){
    		if(A.x>B.x)return A;
    		if(B.x>A.x)return B;
    		return node{A.x,Mod(A.y+B.y)};
    	}
    };
    int n,m;
    int sum,rt;
    bool vis[N];
    int siz[N],maxn[N],dep[N];
    vector<int>To[N],to[N];
    void dfs(int x,int fa){
    	siz[x]=1,maxn[x]=0;
    	for(auto y:To[x]){
    		if(vis[y])continue;
    		if(y==fa)continue;
    		dfs(y,x);
    		siz[x]+=siz[y];
    		maxn[x]=max(maxn[x],siz[y]);
     }
    }
    void getrt(int x,int fa){
    	maxn[x]=max(maxn[x],sum-siz[x]);
    	if(maxn[x]<maxn[rt])rt=x;
    	for(auto y:To[x]){
    		if(vis[y])continue;
    		if(y==fa)continue;
    		getrt(y,x);
    	}
    }
    int build(int x,int d){
    	dfs(x,0);
    	sum=siz[x],rt=x;
    	getrt(x,0);
    	int now=rt;
    	dep[now]=d,vis[now]=1;
    	for(auto y:To[now]){
    		if(vis[y])continue;
    		to[now].push_back(build(y,d+1));
    	}
    	return now;
    }
    int a[N],b[N],s[N];
    node f[1<<9][M];
    void sol(int x){
    	for(int S=s[x];;S=(S-1)&s[x]){
    		int T=S|(1<<dep[x]);
    		for(int i=0;i+a[x]<=m;i++)
    			if(f[S][i].y)f[T][i+a[x]]=f[T][i+a[x]]+(f[S][i]+b[x]);
    		if(!S)break;
    	}
    	for(auto y:to[x])sol(y);
    	for(int S=s[x];;S=(S-1)&s[x]){
    		int T=S|(1<<dep[x]);
    		for(int i=0;i<=m;i++){
    			f[S][i]=f[S][i]+f[T][i];
    			f[T][i]=node{0,0};
    		}
    		if(!S)break;
    	}
    }
    signed main(){
    	freopen("set.in","r",stdin);
    	freopen("set.out","w",stdout);
    	read(n,m);f[0][0]={0,1};
    	for(int i=1;i<=n;i++)read(a[i],b[i]);
    	for(int i=1;i<n;i++){
    		int x,y;
    		read(x,y);
    		To[x].push_back(y),
    		To[y].push_back(x);
    	}
    	rt=build(1,0);
    	for(int i=1;i<=n;i++){
    		s[i]=((1<<dep[i])-1);
    //		assert(dep[i]<9);
    		for(auto j:To[i])if(dep[j]<dep[i])
    			s[i]^=(1<<dep[j]);
    	}
    	sol(rt);
    	for(int i=1;i<m;i++)write(f[0][i].y),putch(' ');write(f[0][m].y),putch('\n');
    	return flush(),0;
    }
    
    • 1

    Information

    ID
    41
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    (None)
    Tags
    (None)
    # Submissions
    0
    Accepted
    0
    Uploaded By