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
题意
给你一棵树,每个点的颜色,问有多少点对满足路径上的所以点颜色数为K。
题解
点分治,把模板的dfsdis改成状态。
代码
 #include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std; #define LL long long
const int maxn=5e4+; vector<int>G[maxn];
int n,k,sumk,a[maxn],p[];
int mx[maxn],size[maxn],vis[maxn],dis[maxn],MIN,num[],cnt,root;
LL ans;
void init()
{
sumk=(<<k)-;
ans=;
for(int i=;i<=n;i++)
{
G[i].clear();
vis[i]=;
}
}
void dfssize(int u,int fa)
{
size[u]=;
mx[u]=;
for(int i=;i<G[u].size();i++)
{
int v=G[u][i];
if(!vis[v]&&v!=fa)
{
dfssize(v,u);
size[u]+=size[v];
mx[u]=max(mx[u],size[v]);
}
}
}
void dfsroot(int r,int u,int fa)
{
if(size[r]-size[u]>mx[u])mx[u]=size[r]-size[u];
if(mx[u]<MIN)MIN=mx[u],root=u;
for(int i=;i<G[u].size();i++)
{
int v=G[u][i];
if(!vis[v]&&v!=fa)dfsroot(r,v,u);
}
}
void dfsdis(int u,int fa,int sta)
{
dis[cnt++]=sta;
num[sta]++;
for(int i=;i<G[u].size();i++)
{
int v=G[u][i];
if(!vis[v]&&v!=fa)
dfsdis(v,u,sta|a[v]);
}
}
LL cal(int r,int sta)
{
cnt=;
memset(num,,sizeof num);
dfsdis(r,r,sta);
for(int i=;i<k;i++)
for(int j=sumk;j>=;j--)
if(!(p[i]&j))
num[j]+=num[j|p[i]];
LL ret=;
for(int i=;i<cnt;i++)ret+=num[sumk^dis[i]];
return ret;
}
void dfs(int u)
{
MIN=n;
dfssize(u,u);
dfsroot(u,u,u);
int Grivate=root;
ans+=cal(Grivate,a[Grivate]);
vis[root]=;
for(int i=;i<G[Grivate].size();i++)
{
int v=G[Grivate][i];
if(!vis[v])
{
ans-=cal(v,a[v]|a[Grivate]);
dfs(v);
}
}
}
int main()
{
p[]=;
for(int i=;i<=;i++)p[i]=p[i-]*;
while(scanf("%d%d",&n,&k)!=EOF)
{
init();
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]=p[a[i]-];
}
int u,v;
for(int i=;i<n;i++)
{
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs();
printf("%lld\n",ans);
}
return ;
}

HDU 5977 Garden of Eden(点分治求点对路径颜色数为K)的更多相关文章

  1. HDU 5977 Garden of Eden (树分治+状态压缩)

    题意:给一棵节点数为n,节点种类为k的无根树,问其中有多少种不同的简单路径,可以满足路径上经过所有k种类型的点? 析:对于路径,就是两类,第一种情况,就是跨过根结点,第二种是不跨过根结点,分别讨论就好 ...

  2. hdu 5977 Garden of Eden(点分治+状压)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5977 题解:这题一看就知道是状压dp然后看了一下很像是点分治(有点明显)然后就是简单的点分治+状压dp ...

  3. HDU 5977 Garden of Eden (树形dp+快速沃尔什变换FWT)

    CGZ大佬提醒我,我要是再不更博客可就连一月一更的频率也没有了... emmm,正好做了一道有点意思的题,就拿出来充数吧=.= 题意 一棵树,有 $ n (n\leq50000) $ 个节点,每个点都 ...

  4. HDU - 5977 Garden of Eden (树形dp+容斥)

    题意:一棵树上有n(n<=50000)个结点,结点有k(k<=10)种颜色,问树上总共有多少条包含所有颜色的路径. 我最初的想法是树形状压dp,设dp[u][S]为以结点u为根的包含颜色集 ...

  5. HDU 5977 Garden of Eden

    题解: 路径统计比较容易想到点分治和dp dp的话是f[i][j]表示以i为根,取了i,颜色数状态为j的方案数 但是转移这里如果暴力转移就是$(2^k)^2$了 于是用FWT优化集合或 另外http: ...

  6. hdu-5977 Garden of Eden(树分治)

    题目链接: Garden of Eden Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/ ...

  7. HDU-5977 - Garden of Eden 点分治

    HDU - 5977 题意: 给定一颗树,问树上有多少节点对,节点对间包括了所有K种苹果. 思路: 点分治,对于每个节点记录从根节点到这个节点包含的所有情况,类似状压,因为K<=10.然后处理每 ...

  8. HDU 1007:Quoit Design(分治求最近点对)

    http://acm.hdu.edu.cn/showproblem.php?pid=1007 题意:平面上有n个点,问最近的两个点之间的距离的一半是多少. 思路:用分治做.把整体分为左右两个部分,那么 ...

  9. HDU5977 Garden of Eden(树的点分治)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5977 Description When God made the first man, he ...

随机推荐

  1. JSON的一些小结

    一.js中 1.json字符串转json对象 var json = $.parseJSON(" {'1':'hello'},{'2':'word'} "); for(var i i ...

  2. immutable.js使用总结

    1. immutable相当于 JSON.parse 和 JSON.stringify: 2.引入redux中,除了 在最外层 reducer中  import { combineReducers } ...

  3. 【DB2】SQL1585N 由于没有具有兼容页面大小的可用系统临时表空间,因此无法创建临时表。SQLSTATE=54048

    自己写了一段SQL,SQL中包含ORDER BY 字句,但是在执行的时候报错如下: 经过查询发现是由于临时表空间的PAGESIZE不够大,可考虑建16k或者32k PAGESIZE的表空间 示例如下: ...

  4. python 保存对象文件

    之前用 R 语言一直感觉 .Rdata 格式的文件很好用,可以把每次执行的中间文件保存便于下次调用,刚熟悉 Python 还没接触这块知识,所以有时候做项目不太顺手,索性上网搜了下,整理如下: 模型存 ...

  5. 自己封装一个弹窗JS

    在我们平时的开发中,一定有很多地方需要用到弹窗提示功能.而系统自带的弹窗奇丑无比,而且我们不能自主控制.因此我们在开发过程中,自己封装一个弹窗JS方便我们使用. 代码demo如下: // JavaSc ...

  6. confluence6.3.1部署+数据迁移

    目录: 环境准备 搭建方法 数据迁移 搭建过程中的bug 1,confluence部署 1.1,环境准备 Java:jdk1.8 mysql: 数据库编码规则选择utf8 -- UTF-8 Unico ...

  7. 【代码问题】SiameseFC

    [SiameseFC]: L Bertinetto, J Valmadre, JF Henriques, et al. Fully-convolutional siamese networks for ...

  8. java中int算法的有趣现象

    今天无意中发现一个怪事,当时没理解,后来跟网友讨论了才知道原理,是关于int值的加法算法,两段代码如下: 代码1: @Test public void test1() { ; ; try { whil ...

  9. [UE4]Named Slot

    用户创建的UI成为其他UI的子控件的时候,默认情况下是不能拥有子控件的,给UI添加一个Named Slot,这个UI就可以拥有子控件 一.创建一个名为testNameSlot的UI,添加3个Named ...

  10. 知识点:Mysql 基本用法之视图

    视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的临时 ...