【HDOJ5977】Garden of Eden(点分治)
题意:给定一棵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(点分治)的更多相关文章
- hdu-5977 Garden of Eden(树分治)
题目链接: Garden of Eden Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/ ...
- 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 ...
- (模板)hdoj5977 Garden of Eden(点分治)
题目链接:https://vjudge.net/problem/HDU-5977 题意:给一颗树,每个结点上有一个权值a[i],a[i]<=10,求有多少条路径满足这条路径上所有权值的结点都出现 ...
- HDU 5977 Garden of Eden (树分治+状态压缩)
题意:给一棵节点数为n,节点种类为k的无根树,问其中有多少种不同的简单路径,可以满足路径上经过所有k种类型的点? 析:对于路径,就是两类,第一种情况,就是跨过根结点,第二种是不跨过根结点,分别讨论就好 ...
- HDU-5977 - Garden of Eden 点分治
HDU - 5977 题意: 给定一颗树,问树上有多少节点对,节点对间包括了所有K种苹果. 思路: 点分治,对于每个节点记录从根节点到这个节点包含的所有情况,类似状压,因为K<=10.然后处理每 ...
- HDU5977 Garden of Eden(树的点分治)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5977 Description When God made the first man, he ...
- Garden of Eden
Garden of Eden Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- uva10001 Garden of Eden
Cellular automata are mathematical idealizations of physical systems in which both space and time ar ...
- hdu 5977 Garden of Eden(点分治+状压)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5977 题解:这题一看就知道是状压dp然后看了一下很像是点分治(有点明显)然后就是简单的点分治+状压dp ...
随机推荐
- Qt之QThread随记
这是一篇随记,排版什么的就没有那么好了:) 首先要知道,一个线程在资源分配完之后是以某段代码为起点开始执行的,例如STL内的std::thread,POSIX下的pthread等,都是以函数加其参数之 ...
- day 52 Django基础一之web框架的本质
Django基础一之web框架的本质 django第一天 本节目录 一 web框架的本质及自定义web框架 二 模板渲染JinJa2 三 MVC和MTV框架 四 Django的下载安装 五 基于D ...
- has value '1.8', but '1.7' is required
使用java7,自己又想在空闲时间学一些java8的新特性,故在安装完1.7之后又安装了1.8 eclispe在启动时报’has value ‘1.8’,but’1.7’ is required’的错 ...
- 在virtualBox中打开vdi(转载)
在VirtualBox中启动“新建虚拟机”向导.第一步,输入名称“ubuntu”,选择系统类型为“Linux 2.6”.第二步,内存大小默认是256MB,不变.第三步,虚拟硬盘,点击按钮“现有”,在新 ...
- POJ 2586 贪心+枚举
Y2K Accounting Bug Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15626 Accepted: 78 ...
- Codeforces Round #461 (Div. 2) C. Cave Painting
C. Cave Painting time limit per test 1 second memory limit per test 256 megabytes Problem Descriptio ...
- Kubespray部署Kubernetes 1.13.0(使用本地镜像仓库)
1. 下载kubespray # git clone https://github.com/kubernetes-sigs/kubespray.git # cd kubespray # pip ins ...
- CentOS下的Redis启动脚本
这是一个Shell脚本,用于管理Redis进程(启动,停止,重启),如果你在使用Redis,这个脚本可供参考. #!/bin/sh # # redis - this script starts and ...
- HDU 4628 Pieces(状态压缩+记忆化搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=4628 题意:给个字符窜,每步都可以删除一个字符窜,问最少用多少步可以删除一个字符窜分析:状态压缩+记忆化搜索 ...
- 用python介绍4种常用的单链表翻转的方法
这里给出了4种4种常用的单链表翻转的方法,分别是: 开辟辅助数组,新建表头反转,就地反转,递归反转 # -*- coding: utf-8 -*- ''' 链表逆序 ''' class ListNod ...