都不好意思写题解了
跑了4000多ms
纪念下自己A的第二题
(我还有一道freetour II wa20多发没A。。。呜呜呜

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define sz(X) ((int)X.size())
  4. #define lson l,m,rt<<1
  5. #define rson m+1,r,rt<<1|1
  6. #define index Index
  7. typedef long long ll;
  8. const int N = 5e4+5;
  9. const int INF = 0x3f3f3f3f;
  10. const double pi = acos(-1.0);
  11. int n,k,K;
  12. ll ans;
  13. int ty[N];
  14. struct Node{
  15. int to,nx;
  16. }E[N<<1];
  17. int head[N], tot, vis[N];
  18. void add(int u,int v) {
  19. E[tot].to = v; E[tot].nx = head[u]; head[u] = tot++;
  20. }
  21. /***************WeightRoot************/
  22. int all, num, center;
  23. int pp[N], nodes[N];
  24. void findRoot(int x,int pre) {
  25. nodes[x] = 1; pp[x] = 0;
  26. for(int i = head[x]; ~i; i = E[i].nx) {
  27. int y = E[i].to; if(y == pre || vis[y]) continue;
  28. findRoot(y,x);
  29. nodes[x] += nodes[y];
  30. pp[x] = max(pp[x], nodes[y]);
  31. }
  32. pp[x] = max(pp[x], all-nodes[x]);
  33. if(pp[x] < num) {
  34. num = pp[x]; center = x;
  35. }
  36. }
  37. int getRoot(int root,int sn) {
  38. num = INF; all = sn; center = root;
  39. findRoot(root, -1);
  40. return center;
  41. }
  42. /****************treecdq**********/
  43. ll has[1050];
  44. ll dp[12][1050];
  45. void getdp(int x, int pre, int num) {
  46. has[num] ++;
  47. for(int i = head[x]; ~i; i = E[i].nx) {
  48. int y = E[i].to; if(y == pre || vis[y]) continue;
  49. getdp(y,x, num|ty[y]);
  50. }
  51. }
  52. ll Cal(int x, int chu) {
  53. ll ret = 0;
  54. memset(has,0,sizeof(has));
  55. getdp(x,x,chu|ty[x]);
  56. for(int i = 0; i <= K; ++i) dp[0][i] = has[i];
  57. for(int i = 1; i <= k; ++i) {
  58. for(int j = 0; j <= K; ++j) {
  59. dp[i][j] = dp[i-1][j];
  60. if(!(j&(1<<(i-1)))) dp[i][j] += dp[i-1][j^(1<<(i-1))];
  61. }
  62. }
  63. for(int i = 0; i <= K; ++i) ret += has[i]* dp[k][i^K];
  64. return ret;
  65. }
  66. void work(int x) {
  67. vis[x] = 1;
  68. ans += Cal(x,0);
  69. for(int i = head[x]; ~i; i = E[i].nx) {
  70. int y = E[i].to; if(vis[y]) continue;
  71. ans -= Cal(y,ty[x]);
  72. work(getRoot(y,nodes[y]));
  73. }
  74. }
  75. int main(){
  76. while(~scanf("%d %d",&n,&k)) {
  77. K = (1<<k)-1;
  78. memset(vis,0,sizeof(vis));
  79. memset(head,-1,sizeof(head)); tot = 0;
  80. for(int i = 1; i <= n; ++i) {
  81. int a; scanf("%d",&a); a--;
  82. ty[i] = 1<<a;
  83. }
  84. for(int i = 1; i < n; ++i) {
  85. int a,b; scanf("%d %d",&a,&b);
  86. add(a, b); add(b, a);
  87. }
  88. ans = 0;
  89. work(getRoot(1,n));
  90. printf("%lld\n", ans);
  91. }
  92. return 0;
  93. }

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. wireshark抓包图解 TCP三次握手/四次挥手详解[转]

    原文链接:http://www.seanyxie.com/wireshark%E6%8A%93%E5%8C%85%E5%9B%BE%E8%A7%A3-tcp%E4%B8%89%E6%AC%A1%E6% ...

  2. JMS基础篇(二)

    简介 异构集成是消息发挥作用的一个领域,大型公司内部可能会遇到很多的平台,Java,.net或者公司自己的平台等. 传送消息还应该支持异步机制,以提高系统整体的性能.异步传输一条消息意味着,发送者不必 ...

  3. POJ Building roads [二分答案 2SAT]

    睡觉啦 #include <iostream> #include <cstdio> #include <cstring> #include <algorith ...

  4. mysql 军规 (转载)

    导语 来自一线的实战经验 每一条军规背后都是血淋淋教训 不要华丽,只要实用 若有一条让你受益,慰矣 主要针对数据库开发人员 总是在灾难发生后,才想起容灾的重要性 总是在吃过亏后,才记得曾经有人提醒过 ...

  5. Windows 定时任务对数据库进行操作

    定时对数据库进行操作可以用mysql的event事件来完成,但是只有mysql5.1后的才支持,所以有一定的局限性,也可以通过其他的mysql管理软件实现,而我发现Windows本身就有个定时任务的功 ...

  6. POI导出多张图片到Excel

    package com.sun.office.excel; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStr ...

  7. mysql 密码过期问题

    问题描述: Your password has expired. To log in you must change it using a client that supports expired p ...

  8. MAC下secretCRT使用技巧(转)

    1.打开secureCRT,按alt+b,可以调出快速启动栏,我相信secureCRT的忠实用户,都会保存一堆的sessions.2.按ctrl,可以同时选中多个session,再点击连接,可快速连接 ...

  9. linux 下创建GRE隧道

    其他国家的互联网如同一个孤岛.要想访问国外网站异常的缓慢,甚至被和谐了.可以建立一条隧道来避免这种情况,下面说说GRE隧道如何建立. 1. GRE介绍 GRE隧道是一种IP-over-IP的隧道,是通 ...

  10. zabbix-agent 启动不起来

    遇到一个问题  zabbix-agent 一直启动不起来 查看Zabbix Agent日志文件才究其原因. tail /var/log/zabbix/zabbix_agentd.logzabbix_a ...