题意:给定一棵n个点的树,每个节点上有一种颜色a[i],一共有k种颜色,问包含所有颜色的路径条数

n<=5e4,k<=10

思路:点分治求方案数

集合并卷积的时候暴力枚举状态即可O(n^logn*2^k)

75e的复杂度 只跑了1.7s 我也是醉了

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
typedef long long ll;
using namespace std;
#define N 110000
#define oo 10000000
#define MOD 1000000007 ll ans;
int head[N],vet[N],nxt[N],a[N],flag[N],son[N],f[N],s[],dep[N],d[N],
n,k,all,tot,sum,root; void add(int a,int b)
{
nxt[++tot]=head[a];
vet[tot]=b;
head[a]=tot;
} void getroot(int u,int fa)
{
son[u]=; f[u]=;
int e=head[u];
while(e)
{
int v=vet[e];
if(v!=fa&&!flag[v])
{
getroot(v,u);
son[u]+=son[v];
f[u]=max(f[u],son[v]);
}
e=nxt[e];
}
f[u]=max(f[u],sum-f[u]);
if(f[u]<f[root]) root=u;
} void getdep(int u,int fa)
{
dep[++dep[]]=d[u];
int e=head[u];
while(e)
{
int v=vet[e];
if(v!=fa&&!flag[v])
{
d[v]=d[u]|(<<a[v]);
getdep(v,u);
}
e=nxt[e];
}
} ll calc(int u,int now)
{
d[u]=now; dep[]=;
getdep(u,);
memset(s,,sizeof(s));
ll ans=;
for(int i=;i<=dep[];i++)
{
for(int j=;j<=all;j++)
if((dep[i]|j)==all) ans+=s[j];
s[dep[i]]++;
}
return ans;
} void solve(int u)
{
ans+=calc(u,<<a[u]);
flag[u]=;
int e=head[u];
while(e)
{
int v=vet[e];
if(!flag[v])
{
ans-=calc(v,(<<a[u])|(<<a[v]));
sum=son[v];
root=;
getroot(v,root);
solve(root);
}
e=nxt[e];
}
} int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
all=(<<k)-;
for(int i=;i<=n;i++) head[i]=flag[i]=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]--;
}
tot=;
for(int i=;i<=n-;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
if(k==)
{
ll ans=n*n;
printf("%I64d\n",ans);
continue;
}
sum=n; f[]=oo; ans=; root=;
getroot(,);
solve(root);
ans*=;
printf("%I64d\n",ans);
}
return ;
}

【HDOJ5977】Garden of Eden(点分治)的更多相关文章

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

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

  2. 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 ...

  3. (模板)hdoj5977 Garden of Eden(点分治)

    题目链接:https://vjudge.net/problem/HDU-5977 题意:给一颗树,每个结点上有一个权值a[i],a[i]<=10,求有多少条路径满足这条路径上所有权值的结点都出现 ...

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

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

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

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

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

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

  7. Garden of Eden

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

  8. uva10001 Garden of Eden

    Cellular automata are mathematical idealizations of physical systems in which both space and time ar ...

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

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

随机推荐

  1. Sum All Primes-freecodecamp算法题目

    Sum All Primes 1.要求 求小于等于给定数值的质数之和. 只有 1 和它本身两个约数的数叫质数.例如,2 是质数,因为它只能被 1 和 2 整除.1 不是质数,因为它只能被自身整除. 2 ...

  2. ethtool查看网卡以及修改网卡配置

    ethtool 命令详解 命令描述: ethtool 是用于查询及设置网卡参数的命令. 使用概要:ethtool ethx       //查询ethx网口基本设置,其中 x 是对应网卡的编号,如et ...

  3. LOL游戏基本代码

    class Hero: def __init__(self, new_nickname, new_aggressivity, new_life_value, new_money, new_armor ...

  4. 【PHP】php中json_decode()和json_encode()

    1.json_decode() json_decode (PHP 5 >= 5.2.0, PECL json >= 1.2.0) json_decode — 对 JSON 格式的字符串进行 ...

  5. mysql的性能优化案例

    在一次项目实现中,以前写了个程序,将在txt文件中的电话号码和对应的类型往数据库中插入,小数据量的情况下,用个数组遍历循环的方式,很容易解决,但是当数据量一下 但是,几十万个电话一次性插入,就变得耗时 ...

  6. 关于Linux系统下zookeeper集群的搭建

    1.集群概述 1.1什么是集群 1.1.1集群概念 集群是一种计算机系统, 它通过一组松散集成的计算机软件和/或硬件连接起来高度紧密地协作完成计算工作.在某种意义上,他们可以被看作是一台计算机.集群系 ...

  7. python * args和** kwargs的用法

    所属网站分类: python基础 > 函数 作者:慧雅 链接: http://www.pythonheidong.com/blog/article/18/ 来源:python黑洞网 www.py ...

  8. 权限组件(15):rbac的使用文档和在业务中的应用

    这里用主机管理系统当做示例. 一.将rbac组件拷贝到项目中. 注意: rbac自己的静态文件.layout.html(被继承的模板).bootstrap.fontsize.公共的css.jquery ...

  9. MySQL基础7-分页查询

    1.分页查询(MySQL特有的,oracle中没有) 栗子1: 每页最多3条记录:pageSize=3:第一页:SELECT * FROM product LIMIT 0,3第二页:SELECT * ...

  10. 【Permutation Sequence】cpp

    题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...