传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5909

【题解】

设$f_{x,i}$表示以$x$节点的子树中,权值为$i$的子树个数,其中$x$必选。

那么有dp方程:$f_{x,i} = \sum_{y = son[x]} f_{x,i} + \sum_{j \oplus k = i}f_{x, j}f_{y, k}$

用FWT优化转移即可,复杂度$O(nmlogm)$。

# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h> using namespace std; typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 1e3 + , H = + ;
const int mod = 1e9+; int n, L, w[N], inv2;
int f[N][H];
int ans[H];
int head[N], nxt[N + N], to[N + N], tot = ;
inline void add(int u, int v) {
++tot; nxt[tot] = head[u]; head[u] = tot; to[tot] = v;
}
inline void adde(int u, int v) {
add(u, v), add(v, u);
} inline int pwr(int a, int b) {
int ret = ;
while(b) {
if(b&) ret = 1ll * ret * a % mod;
a = 1ll * a * a % mod;
b >>= ;
}
return ret;
} int s[H], t[H];
inline void FWT(int *a, int op) {
if(op) {
for (int len = ; len <= L; len<<=) {
int m = len >> ;
for (int *p = a; p != a+L; p += len) {
for (int k=; k<m; ++k) {
int x = p[k], y = p[k+m];
p[k] = 1ll * (x+y) * inv2 % mod;
p[k+m] = 1ll * (x-y+mod) * inv2 % mod;
}
}
}
} else {
for (int len = ; len <= L; len<<=) {
int m = len >> ;
for (int *p = a; p != a+L; p += len) {
for (int k=; k<m; ++k) {
int x = p[k], y = p[k+m];
p[k] = (x+y) % mod;
p[k+m] = (x-y+mod) % mod;
}
}
}
}
} inline void FWT_combine(int *A, int *B) {
for (int i=; i<L; ++i) s[i] = A[i], t[i] = B[i];
FWT(s, ); FWT(t, );
for (int i=; i<L; ++i) s[i] = 1ll * s[i] * t[i] % mod;
FWT(s, );
for (int i=; i<L; ++i) (A[i] += s[i]) %= mod;
} inline void dfs(int x, int fa = ) {
for (int j=; j<L; ++j) f[x][j] = ;
f[x][w[x]] = ;
for (int i=head[x]; i; i=nxt[i]) {
if(to[i] == fa) continue;
dfs(to[i], x);
FWT_combine(f[x], f[to[i]]);
}
for (int j=; j<L; ++j) (ans[j] += f[x][j]) %= mod;
} inline void sol() {
tot = ;
memset(head, , sizeof head);
memset(ans, , sizeof ans);
cin >> n >> L;
for (int i=; i<=n; ++i) scanf("%d", w+i);
for (int i=, u, v; i<n; ++i) {
scanf("%d%d", &u, &v);
adde(u, v);
}
dfs();
printf("%d", ans[]);
for (int i=; i<L; ++i) printf(" %d", ans[i]);
puts("");
} int main() {
int T; cin >> T;
inv2 = pwr(, mod-);
while(T--) sol();
return ;
}

hdu5909 Tree Cutting的更多相关文章

  1. HDU5909 Tree Cutting(树形DP + FWT)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5909 Description Byteasar has a tree T with n ve ...

  2. hdu5909 Tree Cutting 【树形dp + FWT】

    题目链接 hdu5909 题解 设\(f[i][j]\)表示以\(i\)为根的子树,\(i\)一定取,剩余节点必须联通,异或和为\(j\)的方案数 初始化\(f[i][val[i]] = 1\) 枚举 ...

  3. 【HDU5909】Tree Cutting(FWT)

    [HDU5909]Tree Cutting(FWT) 题面 vjudge 题目大意: 给你一棵\(n\)个节点的树,每个节点都有一个小于\(m\)的权值 定义一棵子树的权值为所有节点的异或和,问权值为 ...

  4. 【HDU 5909】 Tree Cutting (树形依赖型DP+点分治)

    Tree Cutting Problem Description Byteasar has a tree T with n vertices conveniently labeled with 1,2 ...

  5. BZOJ3391: [Usaco2004 Dec]Tree Cutting网络破坏

    3391: [Usaco2004 Dec]Tree Cutting网络破坏 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 47  Solved: 37[ ...

  6. BZOJ 3391: [Usaco2004 Dec]Tree Cutting网络破坏( dfs )

    因为是棵树 , 所以直接 dfs 就好了... ---------------------------------------------------------------------------- ...

  7. Tree Cutting

    Tree Cutting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others) Prob ...

  8. 3391: [Usaco2004 Dec]Tree Cutting网络破坏

    3391: [Usaco2004 Dec]Tree Cutting网络破坏 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 76  Solved: 59[ ...

  9. hdu 5909 Tree Cutting [树形DP fwt]

    hdu 5909 Tree Cutting 题意:一颗无根树,每个点有权值,连通子树的权值为异或和,求异或和为[0,m)的方案数 \(f[i][j]\)表示子树i中经过i的连通子树异或和为j的方案数 ...

随机推荐

  1. Scrum 冲刺博客,项目总结

    1.各个成员在 Alpha 阶段认领的任务 数据库环境的搭建,连接数据库:张陈东芳 数据库语句sql语句:张陈东芳 商品实体类的实现:吴敏烽 获取所有商品信息的实现:吴敏烽 根据商品编号获得商品资料: ...

  2. 【重读MSDN之ADO.NET】ADO.NET连接

    连接到ADO.NET中的数据源 在 ADO.NET 中,通过在连接字符串中提供必要的身份验证信息,使用 Connection 对象连接到特定的数据源.使用的 Connection 对象取决于数据源的类 ...

  3. 探讨C++实现一个不可被继承的类

    C#和Java都提供了一种机制让一个类不能被继承,如C#中的sealed关键字和Java的final关键字,然而C++程序员就没这么好命了.不过C++也可以模拟出这种效果,原理基于:子类的构造函数会自 ...

  4. 【bzoj3524】[Poi2014]Couriers 主席树

    题目描述 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. 输入 第一行 ...

  5. [洛谷P4118][Ynoi2016]炸脖龙I([洛谷P3934]Nephren Ruq Insania)

    题目大意:有$n$个数,每个数为$s_i$,两个操作: $1\;l\;r\;x:$表示将区间$[l,r]$内的数加上$x$ $2\;l\;r\;p:$表示求$s_l^{s_{l+1}^{^{s_{l+ ...

  6. [洛谷P3833][SHOI2012]魔法树

    题目大意:给一棵树,路径加,子树求和 题解:树剖 卡点:无 C++ Code: #include <cstdio> #include <iostream> #define ma ...

  7. 【倍增】LCM QUERY

    给一个序列,每次给一个长度l,问长度为l的区间中lcm最小的. 题解:因为ai<60,所以以某个点为左端点的区间的lcm只有最多60种的情况,而且相同的lcm区间的连续的. 所以就想到一个n*6 ...

  8. ARC072E Alice in linear land

    ---题面--- 题解: 首先我们要观察到一个性质,因为在固定的起始距离下,经过固定的操作,最后所在的位置是固定的,我们设经过操作1 ~ i之后所在的地方距离终点为d[i]. 那么如果女巫可以修改第i ...

  9. 【BZOJ1031】字符加密(后缀数组)

    [BZOJ1031]字符加密(后缀数组) 题面 BZOJ 洛谷 题解 把字符串倍长 然后直接求后缀数组, 拍好序之后直接输出就行了. (我只是复习一下\(SA\)而已) #include<ios ...

  10. BZOJ4198 & 洛谷2168 & UOJ130:[NOI2015]荷马史诗——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4198 https://www.luogu.org/problemnew/show/P2168 ht ...