HDU5977 Garden of Eden(树的点分治)

题目

Source

http://acm.hdu.edu.cn/showproblem.php?pid=5977

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.

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

 

分析

题目大概说一棵树,各个结点都有一个数字(<=k),然后问有几条路径使得路径上所有结点包含了1到k的所有数字。

 

路径,自然想到树分治解决,因为任何一条路径都在以某个点为根的子树中且过那条根的链。

 

  • 考虑cnt[S]记录已经统计获得的数字集合为S的路径数量。。
    • 不过这样会发现,新的路径信息与其合并更新答案时,还要枚举它的补集以及补集的超集,这时间复杂度不太好。。
  • 所以直接cnt[S]表示已经统计获得的数字集合为S以及S的超集的路径数量。。
    • 现在问题是新的路径信息如何合并到cnt[S]中,其实只要枚举所有新的路径信息再枚举所有集合状态就OK了,时间复杂度由主定理可知是$O(nlogn2^k)$。。简单粗暴就过了。。

 

另外。。注意路径的起点和终点是可以一样的。。

 

代码

#include
#include
#include
using namespace std;
#define INF (1<<30)
#define MAXN 50010

struct Edge{
    int v,next;
}edge[MAXN<<1];
int NE,head[MAXN];
void addEdge(int u,int v){
    edge[NE].v=v; edge[NE].next=head[u];
    head[u]=NE++;
}

bool vis[MAXN];
int size[MAXN];
void getsize(int u,int fa){
    size[u]=1;
    for(int i=head[u]; i!=-1; i=edge[i].next){
        int v=edge[i].v;
        if(vis[v] || v==fa) continue;
        getsize(v,u);
        size[u]+=size[v];
    }
}
int mini,cen;
void getcen(int u,int fa,int &tot){
    int res=tot-size[u];
    for(int i=head[u]; i!=-1; i=edge[i].next){
        int v=edge[i].v;
        if(vis[v] || v==fa) continue;
        res=max(res,size[v]);
        getcen(v,u,tot);
    }
    if(res 
   

 

转载于:https://www.cnblogs.com/WABoss/p/6036216.html

你可能感兴趣的:(HDU5977 Garden of Eden(树的点分治))