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.
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
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;}
阅读全文
0 0
- hdu5977 Garden of Eden
- hdu5977 Garden of Eden
- HDU5977-Garden of Eden
- HDU5977 Garden of Eden 树分治+高维前缀和
- HDU5977 Garden of Eden 树分治+高维前缀和
- Uva10001 Garden of Eden
- 10001 - Garden of Eden
- uva10001 - Garden of Eden
- uva10001 Garden of Eden
- UVa 10001 - Garden of Eden
- UVA 10001 Garden of Eden
- uva 10001 Garden of Eden
- Garden of Eden uva +回溯
- UVA 10001 Garden of Eden
- UVa 10001 - Garden of Eden
- uva 10001 Garden of Eden (DFS)
- 10001 - Garden of Eden(dfs)
- UVA - 10001 Garden of Eden (回溯)
- RGB、YUV和YCbCr
- JSP页面使用js直接打印页面信息加载水印
- 11.09课堂笔记、作业以及学习心得
- HTML简单的失去焦点表单验证
- python 数据类型跟变量
- HDU5977-Garden of Eden
- com.android.builder.internal.aapt.AaptException: Failed to crunch file解决方案
- Python实现汉诺塔递归经典算法
- c#超市收银系统,多态的使用
- 每天一道LeetCode-----给定大小为n+1的数组,元素大小在[1 : n]之间,只有一个元素会重复出现多次,找到重复的那个
- GMock 与 GTest
- lua study1
- Android学习《第一行代码》03
- RPG问题的妙解