hdu 5977 Garden of Eden(树上点分治)

Garden of Eden

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1844    Accepted Submission(s): 602


 

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.

 

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.

思路:
正常的树上点分治,合并的时候考虑状态枚举,
假设某点到根节点所经过的状态为S,则所有
包含((1<<k)-1)^S状态的集合都满足条件。

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=50005;
vector<int>G[maxn],V;
int a[maxn],n,k,root,now_size;
int son[maxn],K[1030];
bool vis[maxn];
ll ans;
void get_root(int v,int fa,int SIZE)
{
    son[v]=1;
    int ma=0;
    for(int i=0;i<G[v].size();i++)
    {
        int to=G[v][i];
        if(to==fa||vis[to]) continue;
        get_root(to,v,SIZE);
        son[v]+=son[to];
        ma=max(ma,son[to]);
    }
    ma=max(ma,SIZE-son[v]);
    if(ma<now_size)
    {
        now_size=ma;
        root=v;
    }
}
void get_V(int v,int fa,int s)
{
    K[s]++;V.push_back(s);
    for(int i=0;i<G[v].size();i++)
    {
        int to=G[v][i];
        if(to==fa||vis[to]) continue;
        get_V(to,v,s|(1<<a[to]));
    }
}
ll cal(int v,int s)
{
    memset(K,0,sizeof(K));
    V.clear();
    get_V(v,0,s|(1<<a[v]));
    /*for(int S=(1<<k)-1;S>=0;S--)
    {
        for(int i=0;i<k;i++)
        {
            if(!(S>>i&1))
                K[S]+=K[S|(1<<i)];
        }
    }*/ //注意顺序,上面的循环顺序会记重。
    for(int i=0;i<k;i++)
    {
        for(int S=(1<<k)-1;S>=0;S--)
        {
            if(!(S>>i&1)) K[S]+=K[S|(1<<i)];
        }
    }
    ll cnt=0;
    for(int i=0;i<V.size();i++)
        cnt+=K[((1<<k)-1)^V[i]];
    return cnt;
}
void dfs(int v,int fa)
{
    vis[v]=1;
    ans+=cal(v,0);
    for(int i=0;i<G[v].size();i++)
    {
        int to=G[v][i];
        if(to==fa||vis[to]) continue;
        ans-=cal(to,1<<a[v]);
        now_size=1e9;
        get_root(root=to,0,son[to]);
        dfs(root,v);
    }
}
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        ans=0;
        memset(vis,0,sizeof(vis));
        for(int i=0;i<maxn;i++) G[i].clear();
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            a[i]--;
        }
        for(int i=1;i<n;i++)
        {
            int x,y;scanf("%d%d",&x,&y);
            G[x].push_back(y);
            G[y].push_back(x);
        }
        now_size=1e9;
        get_root(root=1,0,n);
        dfs(root,0);
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/albertluf/article/details/81388596