When God made the first man, he put him on a beautiful garden, the Garden of Eden. Here Adam lived with all animals. God gave Adam eternal life. But Adam was lonely in the garden, so God made Eve. When Adam was asleep one night, God took a rib from him and made Eve beside him. God said to them, “here in the Garden, you can do everything, but you cannot eat apples from the tree of knowledge.”
One day, Satan came to the garden. He changed into a snake and went to live in the tree of knowledge. When Eve came near the tree someday, the snake called her. He gave her an apple and persuaded her to eat it. Eve took a bite, and then she took the apple to Adam. And Adam ate it, too. Finally, they were driven out by God and began a hard journey of life.
The above is the story we are familiar with. But we imagine that Satan love knowledge more than doing bad things. In Garden of Eden, the tree of knowledge has n apples, and there are k varieties of apples on the tree. Satan wants to eat all kinds of apple to gets all kinds of knowledge.So he chooses a starting point in the tree,and starts walking along the edges of tree,and finally stops at a point in the tree(starting point and end point may be same).The same point can only be passed once.He wants to know how many different kinds of schemes he can choose to eat all kinds of apple. Two schemes are different when their starting points are different or ending points are different.

Input

There are several cases.Process till end of input.
For each case, the first line contains two integers n and k, denoting the number of apples on the tree and number of kinds of apple on the tree respectively.
The second line contains n integers meaning the type of the i-th apple. Types are represented by integers between 1 and k .
Each of the following n-1 lines contains two integers u and v,meaning there is one edge between u and v.1≤n≤50000, 1≤k≤10

Output

For each case output your answer on a single line.

Sample Input

3 2
1 2 2
1 2
1 3

Sample Output

6

 

 

 

 

 

题解

显然可以点分治

由于颜色数比较小,我们可以状压,这样就可以求出每种状态的路径数有多少

对于一条状态为S的路径我们需要求出S的补集的所有超集的对应的路径数之和

(S的超集T满足S是T的子集)

我们可以直接枚举补集的子集,O(3^k)直接T飞

我们其实可以高维后缀和(超集和)(有点像子集卷积)

把一个全集大小为k的集合视为一个k维空间的一个点

比如说状态0011011可以视为7维空间的点(0,0,1,1,0,1,1)

然后对这个高维空间一维一维地做后缀和,这样的时间复杂度为O(k*(2^k))

剩下的就是点分治了

代码:(调了我一个小时,结果发现状压用的all和点分治求重心的all重名了啊啊啊啊啊啊)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int gi()
{
	char c;int num=0,flg=1;
	while((c=getchar())<'0'||c>'9')if(c=='-')flg=-1;
	while(c>='0'&&c<='9'){num=num*10+c-48;c=getchar();}
	return num*flg;
}
#define N 50005
#define LL long long
const int INF=0x3f3f3f3f;
int n,k,S,a[N];
int fir[N],to[2*N],nxt[2*N],cnt;
void adde(int a,int b)
{
	to[++cnt]=b;nxt[cnt]=fir[a];fir[a]=cnt;
	to[++cnt]=a;nxt[cnt]=fir[b];fir[b]=cnt;
}
LL f[1<<10],ans;
int siz[N],nrt,all,sta[N],dc;bool vis[N];
inline void findrt(int u,int ff)
{
	int mx=0;siz[u]=1;
	for(int v,p=fir[u];p;p=nxt[p]){
		if(!vis[v=to[p]]&&v!=ff){
			findrt(v,u);
			siz[u]+=siz[v];
			mx=max(mx,siz[v]);
		}
	}
	mx=max(mx,all-siz[u]);
	if(2*mx<=all)nrt=u;
}
inline int getrt(int u,int sz)
{
	nrt=-INF;all=sz;
	findrt(u,0);return nrt;
}
void pre(int u,int ff,int s)
{
	f[s|=a[u]]++;
	siz[u]=1;sta[++dc]=s;
	for(int v,p=fir[u];p;p=nxt[p])
		if(!vis[v=to[p]]&&v!=ff)
			pre(v,u,s),siz[u]+=siz[v];
}
LL calc(int u,int s)
{
	dc=0;memset(f,0,sizeof(f));
	pre(u,0,s);
	for(int j=0;j<k;++j)
		for(int i=S;i>=0;i--)
			if(!((1<<j)&i)) f[i]+=f[i|(1<<j)];
	LL res=0;
	for(int i=1;i<=dc;i++)res+=f[sta[i]^S];
	return res;
}
void solve(int u)
{
	vis[u]=1;ans+=calc(u,0);
	for(int v,p=fir[u];p;p=nxt[p])
		if(!vis[v=to[p]])ans-=calc(v,a[u]);
	for(int v,p=fir[u];p;p=nxt[p])
		if(!vis[v=to[p]])solve(getrt(v,siz[v]));
}
int main()
{
	int i,u,v;
    while(~scanf("%d%d",&n,&k)){
		memset(vis,0,sizeof(vis));
		memset(fir,0,sizeof(fir));cnt=0;
		S=(1<<k)-1;ans=0;
		for(i=1;i<=n;i++)a[i]=1<<(gi()-1);
		for(i=1;i<n;i++){u=gi();v=gi();adde(u,v);}
		solve(getrt(1,n));
		printf("%lld\n",ans);
	}
}