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 ...
随机推荐
- CentOS下如何从vi编辑器插入模式退出到命令模式
刚打了下关于vi编辑器的命令,发现一直退出不了.后来自己敲着敲着它就退出了,写博客记录下. 比如现在w文件夹下面有一个ww文件 我进入这个文本,输入命令 vi ww,未回车,情况如下 按了回车,就进入 ...
- Java中UTC时间转换
import java.text.SimpleDateFormat; import java.util.Date; import java util.Calendar; public class Te ...
- 11.solr学习速成之MoreLikeThis
Solr相似匹配 在网页搜索或电商产品搜索结果页面,很多时候会看到一个相似文档.相似产品或找相似的链接.Solr 使用 MoreLikeThisComponent(MLT)和 MoreLikeT ...
- Php函数set_include_path()函数详解
set_include_path--设置include_path配置选项. 说明 string set_include_path(string $new_include_path); 为当前脚本设置i ...
- Python网络编程与并发编程
网络编程基础 黏包 , 并发 计算机网络的发展及基础网络概念 Python 中的进程与 锁 Python IO 多路复用 \协程
- 高效率terminal和sublime 相互启动
在日常的工作中,我们经常使用到terminal和Sublime .今天给大家介绍下怎样高效率的实现terminal和sublime 相互启动 (这里说的是MAC环境,我用的是Sublime Text ...
- linux条件变量
条件变量用于线程之间的通信,和互斥锁一起使用.条件变量用于及时通知等待的线程条件的变化,使线程不至于错过变化. 考虑下面的情况,有AB两个线程对index这个全局变量进行++,一个线程C用于判断,in ...
- TCP/IP协议详解之广播和多播
广播和多播仅应用于 U D P,它们对需将报文同时传往多个接收者的应用来说十分重要.T C P是一个面向连接的协议,它意味着分别运行于两主机(由 I P地址确定)内的两进程(由端口号确定)间存在一条连 ...
- CentOS设置服务开机自动启动【转】
CentOS设置服务开机自动启动[转]Posted on 2012-06-28 16:00 eastson 阅读(4999) 评论(0) 编辑 收藏 CentOS安装好apache.mysql等服务器 ...
- webService 入门级
1,建立一个项目名为Trans,web项目,普通java项目都可以!这里我们就以简单的java应用程序来作为示范吧! 1.1在建立一个方法属于com.shu.function.Function类: / ...