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

本文介绍了一种在知识树上进行点分治的算法,该算法通过枚举状态来找出所有能够吃掉树上所有种类苹果的不同路径方案数量。特别地,文章详细解释了如何通过避免重复计算来提高算法效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值