HDU 5909 Tree Cutting(树形DP+FWT)

Tree Cutting

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 1379    Accepted Submission(s): 538


 

Problem Description

Byteasar has a tree T with n vertices conveniently labeled with 1,2,...,n. Each vertex of the tree has an integer value vi.

The value of a non-empty tree T is equal to v1⊕v2⊕...⊕vn, where ⊕ denotes bitwise-xor.

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

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

 

 

Input

The first line of the input contains an integer 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) and m(1≤m≤210), denoting the size of the tree T and the upper-bound of v.

The second line of the input contains n integers v1,v2,v3,...,vn(0≤vi<m), denoting the value of each node.

Each of the following n−1 lines contains two integers ai,bi, denoting an edge between vertices ai and bi(1≤ai,bi≤n).

It is guaranteed that m can be represent as 2k, where k is a non-negative integer.

 

 

Output

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

The answer is huge, so please module 109+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

 

 

Source

BestCoder Round #88

 

 

Recommend

wange2014   |   We have carefully selected several similar problems for you:  6447 6446 6445 6444 6443 

 

 

Statistic | Submit | Discuss | Note

题意:Byteasar有一棵nn个点的无根树,节点依次编号为11到nn,其中节点ii的权值为v_iv​i​​。 定义一棵树的价值为它所有点的权值的异或和。 现在对于每个[0,m)[0,m)的整数kk,请统计有多少TT的非空连通子树的价值等于kk。 一棵树TT的连通子树就是它的一个连通子图,并且这个图也是一棵树。

题解:如果暴力的话是个裸的 树形DP,但是复杂度是n三方,但是我们发现对于异或运算,我们可以用fwt加速 运算,使得复杂度 降到n*nlogn。

#include<vector>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long 
#define mod 1000000007
const int rev=(mod+1)>>1;
ll dp[1500][1500],ans[1505],sum[1500];
int n,m,a[1505];
vector<int>q[1505];
void fwt(ll *a,int n)
{
    for (int i=1;i<n;i<<=1)
		for (int j=0;j<n;j+=(i<<1))
			for (int k=0,x,y;k<i;++k)
			{
				x=a[j+k],y=a[j+k+i];
				a[j+k]=(x+y)%mod; //if (a[j+k]>=rhl) a[j+k]-=rhl;
				a[j+k+i]=(x-y+mod)%mod;// if (a[j+k+i]<0) a[j+k+i]+=rhl;
				//and:a[j+k]=x+y;
				//or:a[j+k+i]=x+y;
				//xor:a[j+k]=x+y,a[j+k+i]=x-y;
			}
    return;
}
void ufwt(ll *a,int n)
{
    for (int i=1;i<n;i<<=1)
		for (int j=0;j<n;j+=(i<<1))
			for (int k=0,x,y;k<i;++k)
			{
				x=a[j+k],y=a[j+k+i];
				a[j+k]=1ll*(x+y)*rev%mod;
				a[j+k+i]=(1ll*(x-y)*rev%mod+mod)%mod;
				//and:a[j+k]=x-y;
				//or:a[j+k+i]=y-x;
				//xor:a[j+k]=(x+y)/2,a[j+k+i]=(x-y)/2;
			}
    return;
}

void work(ll a[],ll b[],int n)
{
	fwt(a,n);fwt(b,n);
	for(int i=0;i<n;i++)
		a[i]=1ll*a[i]*b[i]%mod;
	ufwt(a,n);
}
void dfs(int u,int p)
{
	dp[u][a[u]]=1;
	for(int i=0;i<q[u].size();i++)
	{
		int v=q[u][i];
		if(v==p)
			continue;
		dfs(v,u);
		for(int j=0;j<m;j++)
			sum[j]=dp[u][j];
		work(dp[u],dp[v],m);
		for(int j=0;j<m;j++)
			dp[u][j]=(dp[u][j]+sum[j])%mod;
	}
	for(int i=0;i<m;i++)
		ans[i]=(ans[i]+dp[u][i])%mod;
}
int main(void)
{
	int T,x,y;
	scanf("%d",&T);
	while(T--)
	{
		memset(dp,0,sizeof(dp));
		memset(ans,0,sizeof(ans));
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
			q[i].clear();
		for(int i=1;i<=n;i++)
			scanf("%d",&a[i]);
		for(int i=1;i<n;i++)
		{
			scanf("%d%d",&x,&y);
			q[x].push_back(y);
			q[y].push_back(x);
		}
		dfs(1,0);
		for(int i=0;i<m-1;i++)
			printf("%lld ",ans[i]);
		printf("%lld\n",ans[m-1]);
	}
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值