HDU 5977 Garden of Eden (树分治+状态压缩)
题意:给一棵节点数为n,节点种类为k的无根树,问其中有多少种不同的简单路径,可以满足路径上经过所有k种类型的点?
析:对于路径,就是两类,第一种情况,就是跨过根结点,第二种是不跨过根结点,分别讨论就好,由于结点比较大,所以采用分治来进行处理,优先选取重点作为划分的依据。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define FOR(x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 50000 + 10;
const LL mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r > 0 && r <= n && c > 0 && c <= m;
}
int all; struct Edge{
int to, next;
}; Edge edge[maxn<<1];
int head[maxn], cnt;
int val[maxn]; void addEdge(int u, int v){
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt++;
} int root, num, f[maxn];
LL dp[1<<10];
int sum[maxn];
bool vis[maxn];
LL ans; void dfs_for_root(int u, int fa){
sum[u] = 1; f[u] = 0;
for(int i = head[u]; ~i; i = edge[i].next){
int v = edge[i].to;
if(v == fa || vis[v]) continue;
dfs_for_root(v, u);
sum[u] += sum[v];
f[u] = max(f[u], sum[v]);
}
f[u] = max(f[u], num - sum[u]);
if(f[root] > f[u]) root = u;
} void dfs_for_color(int u, int fa, int s){
for(int i = head[u]; ~i; i = edge[i].next){
int v = edge[i].to;
if(v == fa || vis[v]) continue;
++dp[s|1<<val[v]];
dfs_for_color(v, u, s|1<<val[v]);
}
} LL solve(int u, int s){
ms(dp, 0);
++dp[s];
dfs_for_color(u, -1, s);
LL ans = 0;
for(int i = 0; i <= all; ++i){
if(!dp[i]) continue;
int tmp = 0;
tmp += dp[all];
for(int j = i; j; j = (j-1)&i)
tmp += dp[all^j];
ans += (LL)tmp * dp[i];
}
return ans;
} void dfs_for_ans(int u){
ans += solve(u, 1<<val[u]);
vis[u] = true;
for(int i = head[u]; ~i; i = edge[i].next){
int v = edge[i].to;
if(vis[v]) continue;
ans -= solve(v, 1<<val[u]|1<<val[v]);
root = 0;
f[0] = num = sum[v];
dfs_for_root(v, u);
dfs_for_ans(root);
}
} int main(){
while(scanf("%d %d", &n, &m) == 2){
for(int i = 1; i <= n; ++i){
scanf("%d", val+i);
--val[i];
}
all = (1<<m) - 1;
ms(head, -1); cnt = 0;
for(int i = 1; i < n; ++i){
int u, v;
scanf("%d %d", &u, &v);
addEdge(u, v);
addEdge(v, u);
}
ms(vis, 0);
root = 0; ans = 0LL;
f[0] = num = n;
dfs_for_root(1, -1);
dfs_for_ans(root);
printf("%I64d\n", ans);
}
return 0;
}
HDU 5977 Garden of Eden (树分治+状态压缩)的更多相关文章
- 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 ...
- 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(点分治+状压)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5977 题解:这题一看就知道是状压dp然后看了一下很像是点分治(有点明显)然后就是简单的点分治+状压dp ...
- HDU 5977 Garden of Eden
题解: 路径统计比较容易想到点分治和dp dp的话是f[i][j]表示以i为根,取了i,颜色数状态为j的方案数 但是转移这里如果暴力转移就是$(2^k)^2$了 于是用FWT优化集合或 另外http: ...
- HDU 5977 Garden of Eden (树形dp+快速沃尔什变换FWT)
CGZ大佬提醒我,我要是再不更博客可就连一月一更的频率也没有了... emmm,正好做了一道有点意思的题,就拿出来充数吧=.= 题意 一棵树,有 $ n (n\leq50000) $ 个节点,每个点都 ...
- HDU - 5977 Garden of Eden (树形dp+容斥)
题意:一棵树上有n(n<=50000)个结点,结点有k(k<=10)种颜色,问树上总共有多少条包含所有颜色的路径. 我最初的想法是树形状压dp,设dp[u][S]为以结点u为根的包含颜色集 ...
- HDU-5977 - Garden of Eden 点分治
HDU - 5977 题意: 给定一颗树,问树上有多少节点对,节点对间包括了所有K种苹果. 思路: 点分治,对于每个节点记录从根节点到这个节点包含的所有情况,类似状压,因为K<=10.然后处理每 ...
- HDU 3605:Escape(最大流+状态压缩)
http://acm.hdu.edu.cn/showproblem.php?pid=3605 题意:有n个人要去到m个星球上,这n个人每个人对m个星球有一个选择,即愿不愿意去,"Y" ...
- HDU 2809 God of War(DP + 状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2809 题目大意:给出战神吕布的初始攻击力ATI.防御力DEF.生命值HP.每升一级增加的攻击力In_A ...
随机推荐
- buffer cache 深度解析
本文首先详细介绍了oracle中buffer cache的概念以及所包含的内存结构.然后结合各个后台进程(包括DBWRn.CKPT.LGWR等)深入介绍了oracle对于buffer cache的管理 ...
- How To Move a MySQL Data Directory to a New Location on Ubuntu 16.04
16 How To Move a MySQL Data Directory to a New Location on Ubuntu 16.04 PostedJuly 21, 2016 62.1kvie ...
- 【合】C#线程
浅谈ThreadPool 线程池 地址:https://www.cnblogs.com/xugang/archive/2010/04/20/1716042.html 相关概念: 线程池可以看做容纳线程 ...
- 转:Ubuntu下用Sublime输入中文
最近用上ubuntu跑theano,碰到的一个问题就是用sublime编辑代码的时候无法输入中文. 读代码经常要写注释不能用中文是在是麻烦. 曾经考虑过使用别的文本编辑器,但是sublime的用户界面 ...
- DataGrid——行高不起作用
问题1:rowStyler 设置行高不起作用: 在 DataGrid 组件中,提供了 rowStyler 函数,用于设置行的css 样式,但是实践发现,对于height设置不起作用.跟踪代码发现如下: ...
- GNU/Linux操作系统总览
计算机科学本科的专业课包括高等数学.离散数学.模拟电子技术.数字电子技术.微机原理.汇编语言原理.高级程序语言.操作系统原理.高级编译原理.嵌入式原理.网络原理.计算机组成与结构等诸多科目.GNU计算 ...
- 安装Apache2
Linux下安装Apache 2.4 2012-08-06 09:36:51| 分类: linux|字号 订阅 本文原创,欢迎转载.转载请在文章明显可见处张贴如下内容:(注意:请保留超链接格 ...
- Git(二):常用 Git 命令清单
转: http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html 我每天使用 Git ,但是很多命令记不住. 一般来说,日常使用只要记住下图 ...
- Etcd的基本使用
etcd 是 CoreOS 团队于 2013 年 6 月发起的开源项目,它的目标是构建一个高可用的分布式键值(key-value)数据库,基于 Go 语言实现,内部采用 raft 协议作为一致性算法. ...
- quartz的配置文件
quartz在运行时默认加载的是工程目录下的quartz.properties文件,如果工程目录下没有quartz.properties文件,它就会去读取quartz jar包下的quartz.pro ...