Garden of Eden
Garden of Eden
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
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.
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
line.
1 2 2
1 2
1 3
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <cassert>
#include <ctime>
#define rep(i,m,n) for(i=m;i<=(int)n;i++)
#define inf 0x3f3f3f3f
#define mod 998244353
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
#define ls (rt<<1)
#define rs (rt<<1|1)
#define all(x) x.begin(),x.end()
const int maxn=1e5+;
const int N=4e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qmul(ll p,ll q,ll mo){ll f=;while(q){if(q&)f=(f+p)%mo;p=(p+p)%mo;q>>=;}return f;}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p%mod;p=p*p%mod;q>>=;}return f;}
int n,m,k,t,c[maxn],p[maxn],son[maxn],sz,root,bit[<<],q[maxn],tmp[<<];
ll ans;
bool vis[maxn];
vi e[maxn];
void getroot(int x,int y)
{
int i;
son[x]=;p[x]=;
rep(i,,e[x].size()-)
{
int z=e[x][i];
if(z==y||vis[z])continue;
getroot(z,x);
son[x]+=son[z];
p[x]=max(p[x],son[z]);
}
p[x]=max(p[x],sz-son[x]);
if(p[x]<p[root])root=x;
}
void getbit(int x,int y)
{
q[x]|=(<<c[x]);
bit[q[x]]++;
int i;
rep(i,,e[x].size()-)
{
int z=e[x][i];
if(z==y||vis[z])continue;
q[z]=q[x];
getbit(z,x);
}
}
ll cal(int x,int &p)//计算集合中位或为(1<<m)-1的对数;
{
memset(bit,,sizeof(bit));
getbit(x,);
memcpy(tmp,bit,sizeof(bit));
int i,j;
rep(i,,m-)
{
rep(j,,(<<m)-)
{
if(j>>i&)continue;
tmp[j]+=tmp[j^(<<i)];
}
}
ll ret=;
rep(i,,(<<m)-)ret+=(ll)bit[i]*tmp[i^((<<m)-)];
return ret;
}
void gao(int x)
{
int i;
q[x]=;
ans+=cal(x,q[x]);
vis[x]=true;
rep(i,,e[x].size()-)
{
int y=e[x][i];
if(!vis[y])
{
q[y]=(<<c[x]);
ans-=cal(y,q[y]);
p[]=sz=son[y];
getroot(y,root=);
gao(root);
}
}
}
int main(){
int i,j;
while(~scanf("%d%d",&n,&m))
{
ans=;
rep(i,,n)
{
scanf("%d",&c[i]);
c[i]--;
e[i].clear();
vis[i]=false;
}
rep(i,,n-)
{
int x,y;
scanf("%d%d",&x,&y);
e[x].pb(y);e[y].pb(x);
}
p[]=sz=n;
getroot(,root=);
gao(root);
printf("%lld\n",ans);
}
return ;
}
Garden of Eden的更多相关文章
- HDU5977 Garden of Eden(树的点分治)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5977 Description When God made the first man, he ...
- hdu-5977 Garden of Eden(树分治)
题目链接: Garden of Eden Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/ ...
- uva10001 Garden of Eden
Cellular automata are mathematical idealizations of physical systems in which both space and time ar ...
- HDU 5977 Garden of Eden(点分治求点对路径颜色数为K)
Problem Description When God made the first man, he put him on a beautiful garden, the Garden of Ede ...
- hdu5977 Garden of Eden
都不好意思写题解了 跑了4000多ms 纪念下自己A的第二题 (我还有一道freetour II wa20多发没A...呜呜呜 #include<bits/stdc++.h> using ...
- HDU 5977 Garden of Eden
题解: 路径统计比较容易想到点分治和dp dp的话是f[i][j]表示以i为根,取了i,颜色数状态为j的方案数 但是转移这里如果暴力转移就是$(2^k)^2$了 于是用FWT优化集合或 另外http: ...
- HDU5977 Garden of Eden 【FMT】【树形DP】
题目大意:求有所有颜色的路径数. 题目分析:参考codeforces997C,先利用基的FMT的性质在$O(2^k)$做FMT,再利用只还原一位的特点在$O(2^k)$还原,不知道为什么网上都要点分治 ...
- HDU 5977 Garden of Eden (树形dp+快速沃尔什变换FWT)
CGZ大佬提醒我,我要是再不更博客可就连一月一更的频率也没有了... emmm,正好做了一道有点意思的题,就拿出来充数吧=.= 题意 一棵树,有 $ n (n\leq50000) $ 个节点,每个点都 ...
- HDU 5977 Garden of Eden (树分治+状态压缩)
题意:给一棵节点数为n,节点种类为k的无根树,问其中有多少种不同的简单路径,可以满足路径上经过所有k种类型的点? 析:对于路径,就是两类,第一种情况,就是跨过根结点,第二种是不跨过根结点,分别讨论就好 ...
随机推荐
- Ubuntu搭建Eclipse+JDK+SDK的Android (转载)
转自:http://blog.csdn.net/ithomer/article/details/6960989 今晚重装Ubuntu系统,重新安装了一套eclipse+jdk+SDK的Android开 ...
- bzoj 1652: [Usaco2006 Feb]Treats for the Cows【区间dp】
裸的区间dp,设f[i][j]为区间(i,j)的答案,转移是f[i][j]=max(f[i+1][j]+a[i](n-j+i),f[i][j-1]+a[j]*(n-j+i)); #include< ...
- 乐搏讲自动化测试- Python自动化测试前景怎么样(4)
Python言语还能够写爬虫,但仅仅只是爬虫的入门罢了.通过Python入门爬虫比较简略易学,不需要在一开始把握太多太根底太底层的常识就能够很快上手,而且很快能够做出成果,十分合适小白一开始想做出点看 ...
- iOS静态库.Framework制作
首先要解释一下什么是库,库(Library)其实就是一段编译好的二进制代码,加上头文件就可以供别人使用,一般会有两种情况要用到库: 某些代码需要给别人使用,但是我们不希望别人看到源码,就需要以库的形式 ...
- Linux 常规操作指南
1.修改Linux服务器别名 临时修改: vim /etc/hostname 修改别名 永久修改: vim /etc/sysconfig/network 添加 HOSTNAME=别名 重启服务器 ...
- 通过路由器的IP映射来解决,两个不同IP地址的PC机之间的从LAN口到WAN口的单向通讯问题
1.问题假设: 在B机中IP地址与子网掩码都固定,网关是路由器的LAN口的IP地址,我们希望通过路由器来实现B机与A机之间的单向通讯问题,也就是说B可以ping通A且可以访问A提供的FTP站点. 2. ...
- C#将类对象转换为字典
主要是实现将类里面 的属性和对应的值转换为字典的键和值. public class RDfsedfw { /// <summary> /// 将匿名类转换为字典 /// </summ ...
- 393 UTF-8 Validation UTF-8 编码验证
详见:https://leetcode.com/problems/utf-8-validation/description/ C++: class Solution { public: bool va ...
- C#模拟百度登录并到指定网站评论回帖(四)
基本的实现功能前面已经全部讲完,基本上可以复制黏贴完成登录百度的过程了 今天的这一贴就说说怎么获取百度的验证码 内容回顾:还记得前面第一贴说的如果登录发生异常,百度会发回2个值吗?是的,就是codeT ...
- ASP.NET中的<%%>介绍
一.主要用于ASP.NET前台绑定用的最多: <%#Eval("")%> <%#Bind("")%> <%=变量%> 1.& ...