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 }
时间: 2024-12-29 15:38:57

fwt优化+树形DP HDU 5909的相关文章

[NOI2014]购票 --- 斜率优化 + 树形DP + 数据结构

题目描述 今年夏天,NOI在SZ市迎来了她30周岁的生日. 来自全国 n 个城市的OIer们都会从各地出发,到SZ市参加这次盛会. 全国的城市构成了一棵以SZ市为根的有根树,每个城市与它的父亲用道路连接. 为了方便起见,我们将全国的 n 个城市用 1 到 n 的整数编号.其中SZ市的编号为 1. 对于除SZ市之外的任意一个城市 v,我们给出了它在这棵树上的父亲城市 fv 以及到父亲城市道路的长度 sv. 从城市 v 前往SZ市的方法为:选择城市 v 的一个祖先 a,支付购票的费用,乘坐交通工具到

树形dp/hdu 1011 Starship Troopers

题意 有n个房子,1号为起点房子.每个房子里会消耗一定的士兵来获取一定的价值.现在有m个士兵,求问可以获得的最大价值 注意:走过的房子不能再走 注意2:若要消灭这个房子的bugs,必须全部消灭 分析 典型的树形dp,01背包,因为每个房子里要么全杀死bugs,要么一个不动,只有取或不取两种状态 设f[i][j]表示以i为根节点,消耗j个士兵所能获得的最大价值 则f[i][j]=max(f[son[i]][k] + f[i][j-k]); 答案为f[1][m] Accepted Code 1 /*

树形DP [HDU 2196] Computer

Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3526    Accepted Submission(s): 1788 Problem Description A school bought the first computer some time ago(so this computer's id is 1). D

树形DP [HDU 1561] The more, The Better

The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5506    Accepted Submission(s): 3274 Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝

(树形DP) hdu 1561

The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5822    Accepted Submission(s): 3469 Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物

(树形DP) hdu 1520

Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5619    Accepted Submission(s): 2580 Problem Description There is going to be a party to celebrate the 80-th Anniversary of the

(树形DP) hdu 5148

Cities Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 579    Accepted Submission(s): 179 Problem Description Long long ago,there is a knight called JayYe.He lives in a small country.This countr

(树形DP) hdu 3452

Bonsai Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 783    Accepted Submission(s): 395 Problem Description After being assaulted in the parking lot by Mr. Miyagi following the "All Valley Kar

(树形DP) hdu 2196

Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3982    Accepted Submission(s): 1996 Problem Description A school bought the first computer some time ago(so this computer's id is 1). Du