HDU5977-Garden of Eden

Garden of Eden

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


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.
 

Sample Input
  
  
3 2 1 2 2 1 2 1 3
 

Sample Output
  
  
6
 

Source
 

Recommend
wange2014
 


题意:给一棵节点数为n,节点种类为k的无根树,问其中有多少种不同的简单路径满足路径经过所有k种类型的点(a->b与b->a算作两条路径,起点与终点也可以相同)

解题思路:树分治,k不超过10,所以可以状压一下,map记录每种情况有几种


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;
const int maxn=50009;

int n,m,u,v;
int s[maxn],nt[maxn*2],e[maxn*2],cnt;
int x[maxn],sum[maxn],mx[maxn],vis[maxn];
map<int,LL>mp;

int dfs(int k,int fa,int p)
{
    int ans=0;
    mx[k]=(sum[k]=1)-1;
    for(int i=s[k];~i;i=nt[i])
    {
        if(e[i]==fa||vis[e[i]]) continue;
        int temp=dfs(e[i],k,p);
        sum[k]+=sum[e[i]];
        mx[k]=max(mx[k],sum[e[i]]);
        if(mx[temp]<mx[ans]) ans=temp;
    }
    mx[k]=max(mx[k],p-sum[k]);
    return mx[k]<mx[ans]?k:ans;
}

LL get(int k,int fa,int p)
{
    LL ans=0;
    for(map<int,LL>::iterator it=mp.begin();it!=mp.end();it++)
    {
        int temp=(*it).first,pp=(1<<m)-1;
        LL g=(*it).second;
        if((temp|p)==pp) ans+=g;
    }
    for(int i=s[k];~i;i=nt[i])
    {
        if(e[i]==fa||vis[e[i]]) continue;
        ans+=get(e[i],k,p|(1<<x[e[i]]));
    }
    return ans;
}

void put(int k,int fa,int p)
{
    mp[p]++;
    for(int i=s[k];~i;i=nt[i])
    {
        if(e[i]==fa||vis[e[i]]) continue;
        put(e[i],k,(1<<x[e[i]])|p);
    }
}

LL Find(int k)
{
    mp.clear();
    mp[0]=1;
    LL ans=0;
    for(int i=s[k];~i;i=nt[i])
    {
        if(vis[e[i]]) continue;
        ans+=get(e[i],k,(1<<x[e[i]])|(1<<x[k]));
        put(e[i],k,1<<x[e[i]]);
    }
    return ans;
}

LL solve(int k,int p)
{
    int y=dfs(k,k,p);
    LL ans=Find(y);
    vis[y]=1;
    for(int i=s[y];~i;i=nt[i])
    {
        if(vis[e[i]]) continue;
        if(sum[e[i]]<sum[y]) ans+=solve(e[i],sum[e[i]]);
        ans+=solve(e[i],p-sum[y]);
    }
    return ans;
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(s,-1,sizeof s);
        memset(vis,0,sizeof vis);
        mx[cnt=0]=INF;
        for(int i=1;i<=n;i++) scanf("%d",&x[i]),x[i]--;
        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&u,&v);
            nt[cnt]=s[u],s[u]=cnt,e[cnt++]=v;
            nt[cnt]=s[v],s[v]=cnt,e[cnt++]=u;
        }
        if(n==1)
        {
            if(m==1) printf("1\n");
            else printf("0\n");
            continue;
        }
        if(m==1)
        {
            printf("%lld\n",1LL*n*n);
            continue;
        }
        printf("%lld\n",2LL*solve(1,n));
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值