Problem Description
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.
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
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
题意
给你一棵树,每个点的颜色,问有多少点对满足路径上的所以点颜色数为K。
题解
点分治,把模板的dfsdis改成状态。
代码
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std; #define LL long long
const int maxn=5e4+; vector<int>G[maxn];
int n,k,sumk,a[maxn],p[];
int mx[maxn],size[maxn],vis[maxn],dis[maxn],MIN,num[],cnt,root;
LL ans;
void init()
{
sumk=(<<k)-;
ans=;
for(int i=;i<=n;i++)
{
G[i].clear();
vis[i]=;
}
}
void dfssize(int u,int fa)
{
size[u]=;
mx[u]=;
for(int i=;i<G[u].size();i++)
{
int v=G[u][i];
if(!vis[v]&&v!=fa)
{
dfssize(v,u);
size[u]+=size[v];
mx[u]=max(mx[u],size[v]);
}
}
}
void dfsroot(int r,int u,int fa)
{
if(size[r]-size[u]>mx[u])mx[u]=size[r]-size[u];
if(mx[u]<MIN)MIN=mx[u],root=u;
for(int i=;i<G[u].size();i++)
{
int v=G[u][i];
if(!vis[v]&&v!=fa)dfsroot(r,v,u);
}
}
void dfsdis(int u,int fa,int sta)
{
dis[cnt++]=sta;
num[sta]++;
for(int i=;i<G[u].size();i++)
{
int v=G[u][i];
if(!vis[v]&&v!=fa)
dfsdis(v,u,sta|a[v]);
}
}
LL cal(int r,int sta)
{
cnt=;
memset(num,,sizeof num);
dfsdis(r,r,sta);
for(int i=;i<k;i++)
for(int j=sumk;j>=;j--)
if(!(p[i]&j))
num[j]+=num[j|p[i]];
LL ret=;
for(int i=;i<cnt;i++)ret+=num[sumk^dis[i]];
return ret;
}
void dfs(int u)
{
MIN=n;
dfssize(u,u);
dfsroot(u,u,u);
int Grivate=root;
ans+=cal(Grivate,a[Grivate]);
vis[root]=;
for(int i=;i<G[Grivate].size();i++)
{
int v=G[Grivate][i];
if(!vis[v])
{
ans-=cal(v,a[v]|a[Grivate]);
dfs(v);
}
}
}
int main()
{
p[]=;
for(int i=;i<=;i++)p[i]=p[i-]*;
while(scanf("%d%d",&n,&k)!=EOF)
{
init();
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]=p[a[i]-];
}
int u,v;
for(int i=;i<n;i++)
{
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs();
printf("%lld\n",ans);
}
return ;
}