1.  

  1.  
题意:
给出一棵树,每个节点都被标记了黑或白色,要求把这棵树的其中k条变切换,划分成k+1棵子树,每颗子树必须有1个黑色节点,求有多少种划分方法。
题解:
树形dp
dp[x][0]表示是以x为根的树形成一块不含黑色点的方案数
dp[x][1]表示是以x为根的树形成一块含一个黑色点方案数
  1. //зїеп:1085422276
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <ctime>
  6. #include <iostream>
  7. #include <algorithm>
  8. #include <set>
  9. #include <vector>
  10. #include <queue>
  11. #include <typeinfo>
  12. #include <map>
  13. #include <stack>
  14. typedef long long ll;
  15. #define inf 100000000
  16. #define mod 1000000007
  17. using namespace std;
  18.  
  19. inline ll read()
  20. {
  21. ll x=,f=;
  22. char ch=getchar();
  23. while(ch<''||ch>'')
  24. {
  25. if(ch=='-')f=-;
  26. ch=getchar();
  27. }
  28. while(ch>=''&&ch<='')
  29. {
  30. x=x*+ch-'';
  31. ch=getchar();
  32. }
  33. return x*f;
  34. }
  35. //***************************************
  36.  
  37. struct ss
  38. {
  39.  
  40. int to,next;
  41. }e[*];
  42. ll dp[][];
  43. int head[],t,vis[],cl[];
  44. void init(){
  45. memset(head,,sizeof(head));
  46. t=;
  47. }
  48. void add(int u,int v)
  49. {
  50. e[t].next=head[u];
  51. e[t].to=v;
  52. head[u]=t++;
  53. }
  54. void dfs(int x,int pre)
  55. {
  56. //vis[x]=1;
  57. dp[x][cl[x]]=;
  58. for(int i=head[x];i;i=e[i].next)
  59. {
  60. if(e[i].to==pre)continue;
  61. dfs(e[i].to,x);
  62. if(cl[x]==){
  63. dp[x][]=(dp[x][]*dp[e[i].to][]%mod+dp[x][]*dp[e[i].to][])%mod;
  64. }
  65. else {
  66.  
  67. dp[x][]=(dp[x][]*dp[e[i].to][]%mod+dp[x][]*dp[e[i].to][]%mod+dp[x][]*dp[e[i].to][])%mod;
  68. dp[x][]=(dp[x][]*dp[e[i].to][]%mod+dp[x][]*dp[e[i].to][])%mod;
  69. }
  70.  
  71. }
  72. }
  73.  
  74. int main()
  75. {
  76. init();
  77. int n;
  78. scanf("%d",&n);
  79. int x;
  80. for(int i=;i<n-;i++){
  81. scanf("%d",&x);
  82. add(i+,x);
  83. add(x,i+);
  84. }
  85. for(int i=;i<n;i++)
  86. scanf("%d",&cl[i]);
  87. dfs(,-);
  88. cout<<dp[][]<<endl;
  89. return ;
  90. }

代码

codeforces Round #263(div2) D. Appleman and Tree 树形dp的更多相关文章

  1. Codeforces 461B. Appleman and Tree[树形DP 方案数]

    B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. CF 461B Appleman and Tree 树形DP

    Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other ...

  3. Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】

    题意:给了一棵树以及每个节点的颜色,1代表黑,0代表白,求将这棵树拆成k棵树,使得每棵树恰好有一个黑色节点的方法数 解法:树形DP问题.定义: dp[u][0]表示以u为根的子树对父亲的贡献为0 dp ...

  4. codeforces 416B. Appleman and Tree 树形dp

    题目链接 Fill a DP table such as the following bottom-up: DP[v][0] = the number of ways that the subtree ...

  5. Bestcoder round #65 && hdu 5593 ZYB's Tree 树形dp

    Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  6. Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)

    题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...

  7. Educational Codeforces Round 58 (Rated for Div. 2) D 树形dp + 数学

    https://codeforces.com/contest/1101/problem/D 题意 一颗n个点的树,找出一条gcd>1的最长链,输出长度 题解 容易想到从自底向长转移 因为只需要g ...

  8. CodeCraft-19 and Codeforces Round #537 (Div. 2) E 虚树 + 树形dp(新坑)

    https://codeforces.com/contest/1111/problem/E 题意 一颗有n个点的树,有q个询问,每次从树挑出k个点,问将这k个点分成m组,需要保证在同一组中不存在一个点 ...

  9. Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)

    https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...

随机推荐

  1. php-fpm.conf两个至关重要的参数

    这里规定了PHP-CGI的连接.发送和读取的时间,300秒足够用了,因此我的服务器很少出现504 Gateway Time-out这个错误.最关键的是php-fpm.conf的设置,这个会直接导致50 ...

  2. Socket网络编程(2)--服务端实现

    中秋了,首先祝大家中秋快乐,闲着无事在家整一个socket的聊天程序,有点仿QQ界面,就是瞎折腾,不知道最后是不是能将所有功能实现. 如果你对socket不了解,请看这篇文章:http://www.c ...

  3. The Flash

    flash.now[:error] = "" render :new flash[:error] = "" redirect videos_path http: ...

  4. linux zookeeper 原理详解

    http://cailin.iteye.com/blog/2014486  直接打开此链接即可 --------------------------------------------------   ...

  5. Python Django 的 django templatedoesnotexist

    django 1.8版本的解决方案 在  setting.py 这个文件里 TEMPLATES = [ ...... #原来的 #'DIRS': [ ], //  这个 列表里添加 template路 ...

  6. MyBatis调用存储过程

    MySQL存储过程 DROP PROCEDURE IF EXISTS transferMoney; -- 实现转账功能的存储过程 CREATE PROCEDURE transferMoney ( IN ...

  7. jlink安装

    https://www.segger.com/jlink-software.html?step=1&file=JLink_510p

  8. 利用cocostudio库函数 实现左右滑动的背包栏UI (cocos2d-x 2.2.0)

    .h #ifndef __COMMON_COMPONENTS__ #define __COMMON_COMPONENTS__ #include "cocos2d.h" #inclu ...

  9. 【转】MySQL Temporary Table相关问题的探究

    本文转载自:http://itindex.net/detail/10901-mysql-temporary-table 问题的引入 让我们先来观察几条非常简单的MySQL语句: mysql> c ...

  10. mysql in 查询优化

    2014年11月29日21:01:01 场景:有的时候查询数据库的select in 语句中会有非常多不连续的数值,会很影响查询效率 方法:将select in 查询转换成多个select betwe ...