HDU5977-Garden of Eden

来源:互联网 发布:淘宝多发了一件怎么办 编辑:程序博客网 时间:2024/06/05 19:01

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 21 2 21 21 3
 

Sample Output
6
 

Source
2016ACM/ICPC亚洲区大连站-重现赛(感谢大连海事大学)
 

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

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果开机密码忘了怎么办 我的ipad很卡怎么办 6s升级后卡顿严重怎么办 软件升级后手机卡顿怎么办 ps通道抠图模糊怎么办 电脑锁屏死机了怎么办 ipad电源键坏了怎么办 华为平板死机黑屏了怎么办 苹果4s锁屏键坏了怎么办 平板电脑密码锁忘记密码怎么办 苹果平板电脑密码锁忘记密码怎么办 平板电脑密码锁密码锁死怎么办 小米平板黑屏按键亮怎么办 小米平板黑屏没反应怎么办 小米平板一刷黑屏了怎么办 小米4卡死了怎么办啊 苹果平板电脑开不了机怎么办 ipad开不了机怎么办都是黑屏 手机突然黑屏了死机状态怎么办 苹果平板死机不能重启怎么办 韩众平板死机了怎么办 苹果平板输入密码多次停用怎么办 ld密码被停用了怎么办 档案被自己丢了怎么办 手机在厂里丢了怎么办 在厂里借工具丢了怎么办 导出的考勤没有姓名怎么办 退休时档案丢了怎么办 职工与企业没有劳资怎么办 去大学报道的档案袋丢失怎么办 档案入学毕业年份写错怎么办 从事业单位辞职后人事档案怎么办 老师辞职不给批怎么办 公办教师去私立学校档案怎么办 辞职后档案不给怎么办 档案不小心拆了怎么办 退休职工档案年龄有涂改怎么办 养老金原始档案找不到怎么办退休 寄辞职信不接收怎么办 公司不给办离职怎么办 离职手续表填写错误怎么办