HDU - 5909 Tree Cutting FWT+树形dp(点分治不会)

Byteasar has a tree TT with nn vertices conveniently labeled with 1,2,...,n1,2,...,n. Each vertex of the tree has an integer value vivi. 

The value of a non-empty tree TT is equal to v1⊕v2⊕...⊕vnv1⊕v2⊕...⊕vn, where ⊕⊕ denotes bitwise-xor. 

Now for every integer kk from [0,m)[0,m), please calculate the number of non-empty subtree of TT which value are equal to kk. 

A subtree of TT is a subgraph of TT that is also a tree.

Input

The first line of the input contains an integer T(1≤T≤10)T(1≤T≤10), denoting the number of test cases. 

In each test case, the first line of the input contains two integers n(n≤1000)n(n≤1000) and m(1≤m≤210)m(1≤m≤210), denoting the size of the tree TT and the upper-bound of vv. 

The second line of the input contains nn integers v1,v2,v3,...,vn(0≤vi<m)v1,v2,v3,...,vn(0≤vi<m), denoting the value of each node. 

Each of the following n−1n−1 lines contains two integers ai,biai,bi, denoting an edge between vertices aiai and bi(1≤ai,bi≤n)bi(1≤ai,bi≤n). 

It is guaranteed that mm can be represent as 2k2k, where kk is a non-negative integer.

Output

For each test case, print a line with mm integers, the ii-th number denotes the number of non-empty subtree of TT which value are equal to ii. 

The answer is huge, so please module 109+7109+7.

Sample Input

2
4 4
2 0 1 3
1 2
1 3
1 4
4 4
0 1 3 1
1 2
1 3
1 4

Sample Output

3 3 2 3
2 4 2 3

 

 

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+100,mod=1e9+7,rev=(mod+1)>>1;
int a[N],dp[N][N],ans[N];
vector <int >mp[N];
int n,m;
int tmp[N];
void FWT(int *a,int n,int t)
{
    for(int d=1; d<n; d<<=1)
        for(int m=d<<1,i=0; i<n; i+=m)
            for(int j=0; j<d; j++)
            {
                int x=a[i+j],y=a[i+j+d];
                if (t==1) a[i+j]=(x+y)%mod,a[i+j+d]=(x-y+mod)%mod;
                else a[i+j]=1LL*(x+y)*rev%mod,a[i+j+d]=(1LL*(x-y)*rev%mod+mod)%mod;
            }
}
void solve(int *a,int *b,int n)
{
    FWT(a,n,1);FWT(b,n,1);
    for(int i=0; i<n; i++)   a[i]=1LL*a[i]*b[i]%mod;
    FWT(a,n,0);
}
void dfs(int x,int fa)
{
    dp[x][a[x]]=1;
    for (int i=0; i<mp[x].size(); i++)
    {
        int v=mp[x][i];
        if(v==fa) continue;
        dfs(v,x);
        
        for (int i=0; i<m; i++) tmp[i]=dp[x][i];
        solve(dp[x],dp[v],m);
        for (int i=0; i<m; i++) dp[x][i]=(dp[x][i]+tmp[i])%mod;
    }
    for (int i=0;i<m;i++) ans[i]=(ans[i]+dp[x][i])%mod;
}


int main()
{
    int T,u,v;
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        memset(dp,0,sizeof dp);
        memset(ans,0,sizeof ans);
        for (int i=1; i<=n; i++) mp[i].clear();
        for (int i=1; i<=n; i++)  scanf("%d",&a[i]);
        for (int i=1; i<n; i++)
        {
            scanf("%d%d",&u,&v);
            mp[u].push_back(v);
            mp[v].push_back(u);
        }
        dfs(1,0);
        for (int i=0;i<m;i++)
            if(i!=0) printf(" %d",ans[i]);
            else printf("%d",ans[i]);
        printf("\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值