1. 程式人生 > >HDU 5909 Tree Cutting(樹形DP+FWT)

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

Recommend

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

題意: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;
}