hdu5977 Garden of Eden
都不好意思写题解了
跑了4000多ms
纪念下自己A的第二题
(我还有一道freetour II wa20多发没A。。。呜呜呜
#include<bits/stdc++.h>
using namespace std;
#define sz(X) ((int)X.size())
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define index Index
typedef long long ll;
const int N = 5e4+5;
const int INF = 0x3f3f3f3f;
const double pi = acos(-1.0);
int n,k,K;
ll ans;
int ty[N];
struct Node{
int to,nx;
}E[N<<1];
int head[N], tot, vis[N];
void add(int u,int v) {
E[tot].to = v; E[tot].nx = head[u]; head[u] = tot++;
}
/***************WeightRoot************/
int all, num, center;
int pp[N], nodes[N];
void findRoot(int x,int pre) {
nodes[x] = 1; pp[x] = 0;
for(int i = head[x]; ~i; i = E[i].nx) {
int y = E[i].to; if(y == pre || vis[y]) continue;
findRoot(y,x);
nodes[x] += nodes[y];
pp[x] = max(pp[x], nodes[y]);
}
pp[x] = max(pp[x], all-nodes[x]);
if(pp[x] < num) {
num = pp[x]; center = x;
}
}
int getRoot(int root,int sn) {
num = INF; all = sn; center = root;
findRoot(root, -1);
return center;
}
/****************treecdq**********/
ll has[1050];
ll dp[12][1050];
void getdp(int x, int pre, int num) {
has[num] ++;
for(int i = head[x]; ~i; i = E[i].nx) {
int y = E[i].to; if(y == pre || vis[y]) continue;
getdp(y,x, num|ty[y]);
}
}
ll Cal(int x, int chu) {
ll ret = 0;
memset(has,0,sizeof(has));
getdp(x,x,chu|ty[x]);
for(int i = 0; i <= K; ++i) dp[0][i] = has[i];
for(int i = 1; i <= k; ++i) {
for(int j = 0; j <= K; ++j) {
dp[i][j] = dp[i-1][j];
if(!(j&(1<<(i-1)))) dp[i][j] += dp[i-1][j^(1<<(i-1))];
}
}
for(int i = 0; i <= K; ++i) ret += has[i]* dp[k][i^K];
return ret;
}
void work(int x) {
vis[x] = 1;
ans += Cal(x,0);
for(int i = head[x]; ~i; i = E[i].nx) {
int y = E[i].to; if(vis[y]) continue;
ans -= Cal(y,ty[x]);
work(getRoot(y,nodes[y]));
}
}
int main(){
while(~scanf("%d %d",&n,&k)) {
K = (1<<k)-1;
memset(vis,0,sizeof(vis));
memset(head,-1,sizeof(head)); tot = 0;
for(int i = 1; i <= n; ++i) {
int a; scanf("%d",&a); a--;
ty[i] = 1<<a;
}
for(int i = 1; i < n; ++i) {
int a,b; scanf("%d %d",&a,&b);
add(a, b); add(b, a);
}
ans = 0;
work(getRoot(1,n));
printf("%lld\n", ans);
}
return 0;
}
hdu5977 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/ ...
- HDU-5977 - Garden of Eden 点分治
HDU - 5977 题意: 给定一颗树,问树上有多少节点对,节点对间包括了所有K种苹果. 思路: 点分治,对于每个节点记录从根节点到这个节点包含的所有情况,类似状压,因为K<=10.然后处理每 ...
- HDU5977 Garden of Eden 【FMT】【树形DP】
题目大意:求有所有颜色的路径数. 题目分析:参考codeforces997C,先利用基的FMT的性质在$O(2^k)$做FMT,再利用只还原一位的特点在$O(2^k)$还原,不知道为什么网上都要点分治 ...
- 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 ...
- Garden of Eden
Garden of Eden Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- (模板)hdoj5977 Garden of Eden(点分治)
题目链接:https://vjudge.net/problem/HDU-5977 题意:给一颗树,每个结点上有一个权值a[i],a[i]<=10,求有多少条路径满足这条路径上所有权值的结点都出现 ...
- HDU 5977 Garden of Eden
题解: 路径统计比较容易想到点分治和dp dp的话是f[i][j]表示以i为根,取了i,颜色数状态为j的方案数 但是转移这里如果暴力转移就是$(2^k)^2$了 于是用FWT优化集合或 另外http: ...
随机推荐
- BZOJ CF388D. Fox and Perfect Sets [线性基 数位DP]
CF388D. Fox and Perfect Sets 题意:求最大元素\(le n\)的线性空间的个数 给神题跪了 orz 容易想到 每个线性基对应唯一的线性空间,我们可以统计满足条件的对应空间不 ...
- POJ1509 Glass Beads [后缀自动机]
题意: 给一个字符串S,每次可以将它的第一个字符移到最后面,求这样能得到的字典序最小的字符串.输出开始下标 练习SAM第一题! SS构造SAM,然后从开始尽量走最小走n步就可以啦 什么?开始位置?!R ...
- Retrofit 实践
Retrofit是一套RESTful架构的Android(Java)客户端实现,基于注解,提供JSON to POJO(Plain Ordinary Java Object,简单Java对象),POJ ...
- (转载)Java:按值传递与按引用传递
原链接:传送门 前天在做系统的时候被Java中参数传递问题卡了一下,回头查阅了相关的资料,对参数传递问题有了新的了解和掌握,但是有个问题感觉还是很模糊,就是Java中到底是否只存在值传递,因为在查阅资 ...
- gitlab10.0安装手记
+ +exec chpst -e /opt/gitlab/etc/gitlab-workhorse/env -P \ + -U git \ + -u git \ + /opt/gitlab/embed ...
- Linux expect自动登录ssh,ftp
[http://blog.51yip.com/linux/1462.html#] #!/usr/bin/expect -f set ip 192.168.1.201 set password meim ...
- dedecms调用文章内容
使用织梦建站时,有时候需要调用某一文档的内容,但织梦默认没有相应的标签,这时就需要我们使用sql语句去抓取了. {dede:sql sql="SELECT aid,typeid,body F ...
- FreeSWITCH 内线拨号 总是使用 dialplan/public 拨号计划,而对 dialplan/default 视而不见
FreeSWITCH 内线拨号 总是使用 dialplan/public 拨号计划,而对 dialplan/default 视而不见 昨天还是 好好的额, 今天 就这样了, 导致 配置都乱了, 搞了 ...
- 让Python输出更漂亮
print 默认输出是换行的,如果要实现不换行需要在变量末尾加上 end="": student_age = 18 print("学生的年龄为:", stude ...
- web自动化一(selenium+python+pycharm环境搭建)
年前公司刚刚搭起了web自动化测试框架的环境,趁着过完年还没全部忘掉,准备把如何搭建环境的方法和大家分享下,有哪里不对的地方,请批评指正,共同进步,共勉! 为此我把搭建环境所需的软件打包上传到百度云, ...