HDU - 5909 Tree Cutting (树形dp+FWT优化)
题意:树上每个节点有权值,定义一棵树的权值为所有节点权值异或的值。求一棵树中,连通子树值为[0,m)的个数。
分析:
设\(dp[i][j]\)为根为i,值为j的子树的个数。
则\(dp[i][j\oplus k] = dp[i][j\oplus k] +dp[i][j] * dp[v][k]\) ,但暴力枚举\(dp[i][j] * dp[v][k]\),每次的复杂度是\(O(M^2)\)的,总的复杂度将是\(O(NM^2)\),N和M都是1e3,不行。
实际上每次要求的,是个异或的卷积。可以用FWT来将复杂度优化至\(O(NMlogM)\)。
#include<bits/stdc++.h>
using namespace std;
const int MAXN = (1<<10)+5;
typedef long long LL;
const int mod= 1e9+7;
LL dp[1005][MAXN];
LL ans[MAXN];
LL a[1005];
int N,M;
LL qpow(LL a,LL n)
{
LL res=1;
while(n){
if(n &1) res = res* a%mod;
a = a*a %mod;
n>>=1;
}
return res;
}
LL rev2 = qpow(2,mod-2);
void FWT(LL a[] ,int n){
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++){
LL x = a[i+j],y = a[i+j+d];
a[i+j] = (x+y)%mod,a[i+j+d] = (x-y+mod)%mod; //取模
}
}
}
}
void UFWT(LL a[],int n){
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++){
LL x = a[i+j],y = a[i+j+d];
a[i+j] = (x+y)*rev2%mod,a[i+j+d] = ((x-y)*rev2%mod + mod) % mod; //取模的情况
}
}
}
}
void solve(LL a[],LL b[],int n){
FWT(a,n);
FWT(b,n);
for (int i = 0 ; i<n ; i++)
a[i]=a[i]*b[i] %mod; //取模
UFWT(a,n);
}
struct Edge{
int v,next;
}edges[2005];
int head[1005],tot;
void init(){
tot = 0;
memset(head,-1,sizeof(head));
}
void AddEdge(int u,int v)
{
edges[tot] = (Edge){v,head[u]};
head[u] = tot++;
}
LL tmp[MAXN];
void dfs(int u,int fa)
{
dp[u][a[u]] = 1;
for(int i=head[u];~i;i=edges[i].next){
int v = edges[i].v;
if(v== fa) continue;
dfs(v,u);
for(int i=0;i<M;++i){
tmp[i] = dp[u][i];
}
solve(dp[u],dp[v],M);
for(int i=0;i<M;++i){
dp[u][i] = (dp[u][i] + tmp[i])%mod; //将之前
}
}
for(int i=0;i<M;++i){
ans[i] = (ans[i]+ dp[u][i]) %mod;
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int T; scanf("%d",&T);
while(T--){
init();
memset(dp,0,sizeof(dp));
memset(ans,0,sizeof(ans));
scanf("%d %d",&N, &M);
for(int i=1;i<=N;++i){
scanf("%lld",&a[i]);
}
for(int i=1,u,v;i<=N-1;++i){
scanf("%d %d",&u,&v);
AddEdge(u,v);
AddEdge(v,u);
}
dfs(1,-1);
for(int i=0;i<M;++i){
printf("%lld%c",ans[i],i==M-1?'\n':' ');
}
}
return 0;
}
HDU - 5909 Tree Cutting (树形dp+FWT优化)的更多相关文章
- hdu 5909 Tree Cutting [树形DP fwt]
hdu 5909 Tree Cutting 题意:一颗无根树,每个点有权值,连通子树的权值为异或和,求异或和为[0,m)的方案数 \(f[i][j]\)表示子树i中经过i的连通子树异或和为j的方案数 ...
- HDU.5909.Tree Cutting(树形DP FWT/点分治)
题目链接 \(Description\) 给定一棵树,每个点有权值,在\([0,m-1]\)之间.求异或和为\(0,1,...,m-1\)的非空连通块各有多少个. \(n\leq 1000,m\leq ...
- hdu 5909 Tree Cutting——点分治(树形DP转为序列DP)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5909 点分治的话,每次要做一次树形DP:但时间应该是 siz*m2 的.可以用 FWT 变成 siz*ml ...
- HDU 5909 Tree Cutting 动态规划 快速沃尔什变换
Tree Cutting 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5909 Description Byteasar has a tree T ...
- [poj3107/poj2378]Godfather/Tree Cutting树形dp
题意:求树的重心(删除该点后子树最大的最小) 解题关键:想树的结构,删去某个点后只剩下它的子树和原树-此树所形成的数,然后第一次dp求每个子树的节点个数,第二次dp求解答案即可. 此题一开始一直T,后 ...
- POJ 2378.Tree Cutting 树形dp 树的重心
Tree Cutting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4834 Accepted: 2958 Desc ...
- HDU 5909 Tree Cutting(FWT+树形DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5909 [题目大意] 给出一棵树,其每棵连通子树的价值为其点权的xor和, 问有多少连通子树的价值为 ...
- poj 2378 Tree Cutting 树形dp
After Farmer John realized that Bessie had installed a "tree-shaped" network among his N ( ...
- hdu 5909 Tree Cutting —— 点分治
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5909 点分治,每次的 rt 是必选的点: 考虑必须选根的一个连通块,可以DP,决策就是在每个子树中决定选不 ...
随机推荐
- VS2008 对话框编辑器“即时预览”
之前在VS2008中利用资源编辑器修改完对话框资源后,总是重新编译一下,然后Ctrl+F5运行来预览修改的效果,不断修改,不断编译,导致很费时,效率低下. 今天,发现了一个很好用的功能“Test Di ...
- web.xml 中 classpath 写法说明
简单理解,classpath就是代表 /WEB-INF /classes/ 这个路径(如果不理解该路径,就把一个web工程发布为war包,然后用winrar查看其包内路径就理解啦) 常用的场景: ...
- Spring Cache 自定义注解
1.在使用spring cache注解如cacheable.cacheevict.cacheput过程中有一些问题: 比如,我们在查到一个list后,可以将list缓存到一个键对应的区域里:当新增.修 ...
- Android 模拟机出现Installation failed due to invalid URI!错误
[2017-03-28 09:52:13 - DataVDemo06] Installation failed due to invalid URI![2017-03-28 09:52:13 - Da ...
- 敏捷开发Scrum学习
官方:http://baike.baidu.com/link?url=VGFzdJpuHX3g90kIX6l1QABWMmBNyf30sTGuEcJ6OJVMq0Cot1G9Imbu1gls-xpI6 ...
- 统计nginx进程占用的物理内存
#!/usr/bin/env python #-*- coding:utf-8 -*- ''' 统计nginx进程占用的物理内存 ''' import os import sys import sub ...
- U盘插入拔出提示
Unit Unit1; Interface Uses Windows, Messages, SysUtils, Variants, classes, Graphics, Controls, Forms ...
- Linux云服务器下Tomcat部署超详细
基于阿里云Centos 7服务器的Tomcat 项目部署 工具:一台安装jdk1.8的Centos 6/7.X 云服务器(64位) Putty ssh远程连接云服务器的软件 FileZillaCli ...
- FZU Moon Game(几何)
Accept: 710 Submit: 2038 Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description Fa ...
- 自定义DataSet
//创建数据集 DataSet dataSet = new DataSet(); //创建虚拟数据表 DataTable datatable = new DataTable(); //获取列集合,添加 ...