都不好意思写题解了
跑了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的更多相关文章

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

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

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

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

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

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

  4. HDU5977 Garden of Eden 【FMT】【树形DP】

    题目大意:求有所有颜色的路径数. 题目分析:参考codeforces997C,先利用基的FMT的性质在$O(2^k)$做FMT,再利用只还原一位的特点在$O(2^k)$还原,不知道为什么网上都要点分治 ...

  5. uva10001 Garden of Eden

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

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

  7. Garden of Eden

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

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

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

  9. HDU 5977 Garden of Eden

    题解: 路径统计比较容易想到点分治和dp dp的话是f[i][j]表示以i为根,取了i,颜色数状态为j的方案数 但是转移这里如果暴力转移就是$(2^k)^2$了 于是用FWT优化集合或 另外http: ...

随机推荐

  1. 济南清北学堂游记 Day 4.

    不知不觉,行程已经过半了啊. 基本上已经熟悉了这里的环境,这其实也意味着我应该很快就要走了. 明天和后天还有最后四场模拟赛,虽然以我的实力拿不到奖,但我也会尽力做好我自己. 我大概反思了一下这几天,其 ...

  2. 使用正则表达式和数组形式获取get方法传入的值

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. 解决asp.net MVC中 当前上下文中不存在名称“model” 的问题

    在vs2013下打开同事上传的cshtml视图文件报错,出现当前上下文中不存在名称“model”,ViewBag,Url等等,在视图中也没有智能提示了:但是不影响编译运行,只是开发时候比较麻烦,但在同 ...

  4. Python中什么时候使用生成器?

    编者注:本文主要参考了<Python核心编程(第二版)> 说到生成器,先说说列表解析.列表解析可以动态创建列表. [expr for iter_var in iterable if con ...

  5. new day

    Learning English is so long,but my english still is not good.I hope that one day I can speak english ...

  6. ios开发常用RGB色值

    iOS中RGB常用的色值,同时可将对颜色的设置定义成宏,方便开发应用,如: // RGB颜色转换(16进制->10进制) #define UIColorFromRGB(rgbValue) [UI ...

  7. Go解析写死的json

    func TestAliAfpAdapter_AskAd_Banner(t *testing.T) { apiData := getApiData() apiData.ApiInfo.ApiPosit ...

  8. 让互联网更快:新一代QUIC协议在腾讯的技术实践分享

    本文来自腾讯资深研发工程师罗成在InfoQ的技术分享. 1.前言 如果:你的 App,在不需要任何修改的情况下就能提升 15% 以上的访问速度,特别是弱网络的时候能够提升 20% 以上的访问速度. 如 ...

  9. JSTL的相关使用

    index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UT ...

  10. Egret学习笔记 (Egret打飞机-5.实现子弹对象)

    上一章把飞机添加到屏幕上,但是飞机要发射子弹对吧?那么这一章我们就来实现一下发射子弹,并实现一个简单的子弹对象池 先来捋一捋思路 1.创建一个子弹对象 2.然后添加一个bitmap,显示子弹贴图 3. ...