HDU5977 Garden of Eden(點分治+高維後綴和(超集和))

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);
	}
}

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章