fwt优化+树形DP HDU 5909
1 //fwt优化+树形DP HDU 5909 2 //见官方题解 3 // BestCoder Round #88 http://bestcoder.hdu.edu.cn/ 4 5 #include <bits/stdc++.h> 6 // #include <iostream> 7 // #include <cstdio> 8 // #include <cstdlib> 9 // #include <algorithm> 10 // #include <vector> 11 // #include <queue> 12 // #include <math.h> 13 using namespace std; 14 #define LL long long 15 typedef pair<int,int> pii; 16 const int inf = 0x3f3f3f3f; 17 const int MOD =1e9+7; 18 const int N =1e3+50; 19 #define clc(a,b) memset(a,b,sizeof(a)) 20 const double eps = 1e-8; 21 void fre() {freopen("in.txt","r",stdin);} 22 void freout() {freopen("out.txt","w",stdout);} 23 inline int read() {int x=0,f=1;char ch=getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch=getchar();}while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}return x*f;} 24 const int rev = (MOD+1)>>1; 25 int a[N],tem[N]; 26 int n,m; 27 vector<int> g[N]; 28 int dp[N][N]; 29 int ans[N]; 30 void FWT(int *a,int n) { 31 for(int d=1; d<n; d<<=1) 32 for(int m=d<<1,i=0; i<n; i+=m) 33 for(int j=0; j<d; j++) { 34 int x=a[i+j],y=a[i+j+d]; 35 a[i+j]=(x+y)%MOD,a[i+j+d]=(x-y+MOD)%MOD; 36 } 37 } 38 39 void UFWT(int *a,int n) { 40 for(int d=1; d<n; d<<=1) 41 for(int m=d<<1,i=0; i<n; i+=m) 42 for(int j=0; j<d; j++) { 43 int x=a[i+j],y=a[i+j+d]; 44 a[i+j]=1LL*(x+y)*rev%MOD,a[i+j+d]=(1LL*(x-y)*rev%MOD+MOD)%MOD; 45 } 46 } 47 48 void solve(int *a,int *b,int n) { 49 FWT(a,n); 50 FWT(b,n); 51 for(int i=0; i<n; i++) a[i]=1LL*a[i]*b[i]%MOD; 52 UFWT(a,n); 53 } 54 55 void dfs(int u,int f){ 56 dp[u][a[u]]=1; 57 for(int i=0;i<(int)g[u].size();i++){ 58 int v=g[u][i]; 59 if(v==f) continue; 60 dfs(v,u); 61 for(int j=0;j<m;j++) tem[j]=dp[u][j]; 62 solve(dp[u],dp[v],m); 63 for(int j=0;j<m;j++) dp[u][j]=(dp[u][j]+tem[j])%MOD; 64 } 65 for(int i=0;i<m;i++) ans[i]=(ans[i]+dp[u][i])%MOD; 66 } 67 int main(){ 68 // fre(); 69 int T; 70 scanf("%d",&T); 71 while(T--){ 72 clc(dp,0); 73 clc(ans,0); 74 scanf("%d%d",&n,&m); 75 for(int i=1;i<=n;i++) { 76 scanf("%d",&a[i]); 77 g[i].clear(); 78 } 79 for(int i=1;i<=n-1;i++){ 80 int u,v; 81 scanf("%d%d",&u,&v); 82 g[u].push_back(v); 83 g[v].push_back(u); 84 } 85 dfs(1,0); 86 for(int i=0;i<m;i++){ 87 printf("%d%c",ans[i],i==m-1 ? '\n':' '); 88 } 89 } 90 return 0; 91 }