Tree Cutting

Time Limit: 4000/2000 MS (Java/Others)
Memory Limit: 262144/131072 K (Java/Others)
Problem Description

Byteasar has a tree TTT with nnn vertices conveniently labeled with 1,2,...,n1,2,...,n1,2,...,n. Each vertex of the tree has an integer value viv_iv​i​​.

The value of a non-empty tree TTT is equal to v1⊕v2⊕...⊕vnv_1\oplus v_2\oplus ...\oplus v_nv​1​​⊕v​2​​⊕...⊕v​n​​, where ⊕\oplus⊕ denotes bitwise-xor.

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

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

Input

The first line of the input contains an integer T(1≤T≤10)T(1\leq T\leq10)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\leq 1000)n(n≤1000) and m(1≤m≤210)m(1\leq m\leq 2^{10})m(1≤m≤2​10​​), denoting the size of the tree TTT and the upper-bound of vvv.

The second line of the input contains nnn integers v1,v2,v3,...,vn(0≤vi<m)v_1,v_2,v_3,...,v_n(0\leq v_i < m)v​1​​,v​2​​,v​3​​,...,v​n​​(0≤v​i​​<m), denoting the value of each node.

Each of the following n−1n-1n−1 lines contains two integers ai,bia_i,b_ia​i​​,b​i​​, denoting an edge between vertices aia_ia​i​​ and bi(1≤ai,bi≤n)b_i(1\leq a_i,b_i\leq n)b​i​​(1≤a​i​​,b​i​​≤n).

It is guaranteed that mmm can be represent as 2k2^k2​k​​, where kkk is a non-negative integer.

Output

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

The answer is huge, so please module 109+710^9+710​9​​+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 分析:dp[i][j]表示以i为根异或值为j的方案数;
   在加入i的儿子x的子树方案时,dp[i][j]=dp[i][j]+dp[i][k]*dp[x][t](k^t=j);
   其中dp[i][k]*dp[x][t](k^t=j)的复杂度为n²,可以用异或卷积加速到nlogn;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define vi vector<int>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define rev (mod+1)/2
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
using namespace std;
const int maxn=1e3+;
int n,m,k,t,a[maxn],dp[maxn][maxn],tmp[maxn],ans[maxn];
vi e[maxn];
void fwt(int *a,int n)
{
for(int d=;d<n;d<<=)
for(int m=d<<,i=;i<n;i+=m)
for(int j=;j<d;j++)
{
int 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(int *a,int n)
{
for(int d=;d<n;d<<=)
for(int m=d<<,i=;i<n;i+=m)
for(int j=;j<d;j++)
{
int x=a[i+j],y=a[i+j+d];
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);
fwt(b,n);
for(int i=;i<n;i++)a[i]=1LL*a[i]*b[i]%mod;
ufwt(a,n);
}
void dfs(int now,int pre)
{
dp[now][a[now]]=;
for(int x:e[now])
{
if(x==pre)continue;
dfs(x,now);
for(int i=;i<m;i++)
tmp[i]=dp[now][i];
solve(dp[now],dp[x],m);
for(int i=;i<m;i++)
dp[now][i]=(dp[now][i]+tmp[i])%mod;
}
for(int i=;i<m;i++)
ans[i]=(ans[i]+dp[now][i])%mod;
}
int main()
{
int i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
rep(i,,n)
{
scanf("%d",&a[i]),e[i].clear();
rep(j,,m-)dp[i][j]=;
}
rep(i,,m-)ans[i]=;
rep(i,,n-)
{
scanf("%d%d",&j,&k);
e[j].pb(k),e[k].pb(j);
}
dfs(,);
rep(i,,m-)printf("%d%c",ans[i],i<m-?' ':'\n');
}
//system ("pause");
return ;
}

Tree Cutting的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. POJ 2378.Tree Cutting 树形dp 树的重心

    Tree Cutting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4834   Accepted: 2958 Desc ...

  7. 【HDU5909】Tree Cutting(FWT)

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

  8. HDU 5909 Tree Cutting 动态规划 快速沃尔什变换

    Tree Cutting 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5909 Description Byteasar has a tree T ...

  9. POJ 2378 Tree Cutting 3140 Contestants Division (简单树形dp)

    POJ 2378 Tree Cutting:题意 求删除哪些单点后产生的森林中的每一棵树的大小都小于等于原树大小的一半 #include<cstdio> #include<cstri ...

随机推荐

  1. scale相关设置—手动设置

    在ggplot2 中,可以进行手动设置的函数有: scale_colour_manual(..., values).scale_fill_manual(..., values). scale_size ...

  2. kettle连接Hbase中数据导出(7)

    http://wiki.pentaho.com/display/BAD/Extracting+Data+from+HBase+to+Load+an+RDBMS 1)新建转换——Big Data——Hb ...

  3. 在IE8中如何通过javascripts改变<style />中的内容?

    1.createStyleSheet() if(document.createStyleSheet){  var cssStyle=document.createStyleSheet();  //兼容 ...

  4. 创建 .gitignore 文件过滤规

    文件 .gitignore 的格式规范如下: 所有空行或者以注释符号 # 开头的行都会被 Git 忽略. 可以使用标准的 glob 模式匹配. 匹配模式最后跟反斜杠(/)说明要忽略的是目录. 要忽略指 ...

  5. dell 去鼠标版功能widnows

    桌面计算机(点击右键)----管理----设备管理器-----鼠标------选择触摸板(ps/2 兼容鼠标)---右击------跟新驱动-------浏览计算机查找------从计算机列表中选择- ...

  6. NOIP2011-普及组复赛-第二题-统计单词数

    题目描述 Description 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数.  现在,请你编程实现这一功能,具体要求是:给 ...

  7. 关于解决“No matching provisioning profiles found”问题-ios

    xcode7之后真机调试就可以不需要调试证书了,但其中也会遇到一些问题令人挠头搔耳.记录下来是给自己提供方便,也为初遇到此问题的人提供解答,利人利己的事情我做!   上图: 图一 本人有一种视警号为e ...

  8. 隔一段时间应用就会挂掉(进程在,但停止响应,也无log输出),必须重启tomcat

    此处是转载的  是给自己做的备注 问题:隔一段时间应用就会挂掉(进程在,但停止响应,也无log输出),必须重启tomcat 原因查找:由于tomcat自身log中并无错误产生,磁盘空间足够,读写也正常 ...

  9. 不合法语句 self.contentView.frame.origin.x = x;

    下面的写法是错误的: CGFloat x = self.contentView.frame.origin.x; x = _lastDownX + translation.x; self.content ...

  10. 笨方法学python--读文件

    1 raw_input()的另外用法 prompt = '> ' likes = raw_input(prompt) 提示符> 可以不用每次输入,要修改,只修改一个地方. 2 读取文件 t ...