题目链接

D. Appleman and Tree

time limit per test :2 seconds
memory limit per test: 256 megabytes
input :standard input
output:standard output

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

Consider a set consisting of k (0 ≤ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into(k + 1) parts. Note, that each part will be a tree with colored vertices.

Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).

Input

The first line contains an integer n (2  ≤ n ≤ 105) — the number of tree vertices.

The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 ≤ pi ≤ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.

The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.

Output

Output a single integer — the number of ways to split the tree modulo 1000000007 (109 + 7).

Sample test(s)
input
  1. 3
    0 0
    0 1 1
output
  1. 2
input
  1. 6
    0 1 1 0 4
    1 1 0 0 1 0
output
  1. 1
input
  1. 10
    0 1 2 1 4 4 4 0 8
    0 0 0 1 0 1 1 0 0 1
output
  1. 27

题意:对每个节点染色,白或者黑,问你断开某些边,使得每个联通块都恰好只有一个节点时黑色,问有多少种断边方式。

思路 :树形DP,  dp[i][0]代表到 i 这个点它所在的子树只有一个黑点的情况,dp[i][0] 包含i节点的这部分没有黑点的情况数。

对于每个节点 i,计算到它的一个子树(根节点u) (设连接的边为edge)的时候,dp[i][0] 为dp[i][0] * dp[u][1] + dp[i][0] * dp[u][0], 已处理完的一定要取dp[i][0], 如果取edge 则子树取dp[u][0],如果不取edge, 则子树取dp[u][1].

dp[i][1] 为 dp[i][1] *(dp[u][0] + dp[u][1]) + dp[i][0] *dp[u][1] , 如果处理完的取dp[i][1],edge取的话为dp[u][0], 不取的话为dp[u][1]; 如果处理完的取dp[i][0], edge一定要取且要乘以dp[u][1]  (ps: dp[u][0] 不能要,如果要的话 u点的部分会出现不含黑点的情况)

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #define mod 1000000007
  5.  
  6. using namespace std ;
  7.  
  8. struct node
  9. {
  10. int u ;
  11. int v ;
  12. int next ;
  13. }p[];
  14. int cnt,head[],color[] ;
  15. long long dp[][] ;
  16.  
  17. void addedge(int u,int v)
  18. {
  19. p[cnt].u = u ;
  20. p[cnt].v = v ;
  21. p[cnt].next = head[u] ;
  22. head[u] = cnt ++ ;
  23. }
  24. void DFS(int u)
  25. {
  26. dp[u][color[u]] = ;
  27. for(int i = head[u] ; i+ ; i = p[i].next)
  28. {
  29. int v = p[i].v ;
  30. DFS(v) ;
  31. dp[u][] = ((dp[u][] * dp[v][]) % mod + (dp[u][] * dp[v][]) % mod + (dp[u][] * dp[v][]) % mod) % mod ;
  32. dp[u][] = ((dp[u][] * dp[v][]) % mod + (dp[u][] * dp[v][]) % mod) % mod ;
  33. }
  34. }
  35. int main()
  36. {
  37. int n ,a;
  38. while(~scanf("%d",&n))
  39. {
  40. cnt = ;
  41. memset(head,-,sizeof(head)) ;
  42. memset(dp,,sizeof(dp)) ;
  43. for(int i = ; i < n ; i++)
  44. {
  45. scanf("%d",&a) ;
  46. addedge(a,i) ;
  47. }
  48. for(int i = ; i < n ; i++)
  49. scanf("%d",&color[i]) ;
  50. DFS() ;
  51. printf("%I64d\n",dp[][]) ;
  52. }
  53. return ;
  54. }

Codeforces Round #263 (Div. 2) D. Appleman and Tree(树形DP)的更多相关文章

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

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

  2. 贪心 Codeforces Round #263 (Div. 2) C. Appleman and Toastman

    题目传送门 /* 贪心:每次把一个丢掉,选择最小的.累加求和,重复n-1次 */ /************************************************ Author :R ...

  3. Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper 树状数组暴力更新

    C. Appleman and a Sheet of Paper   Appleman has a very big sheet of paper. This sheet has a form of ...

  4. Codeforces Round #263 (Div. 2) A. Appleman and Easy Task【地图型搜索/判断一个点四周‘o’的个数的奇偶】

    A. Appleman and Easy Task time limit per test 1 second memory limit per test 256 megabytes input sta ...

  5. Codeforces Round #196 (Div. 2) D. Book of Evil 树形dp

    题目链接: http://codeforces.com/problemset/problem/337/D D. Book of Evil time limit per test2 secondsmem ...

  6. Codeforces Round #382 (Div. 2) 继续python作死 含树形DP

    A - Ostap and Grasshopper zz题能不能跳到  每次只能跳K步 不能跳到# 问能不能T-G  随便跳跳就可以了  第一次居然跳越界0.0  傻子哦  WA1 n,k = map ...

  7. Codeforces Round #419 (Div. 1) C. Karen and Supermarket 树形DP

    C. Karen and Supermarket     On the way home, Karen decided to stop by the supermarket to buy some g ...

  8. Codeforces Round #267 (Div. 2) C. George and Job(DP)补题

    Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...

  9. Codeforces Round #263 (Div. 2)

    吐槽:一辈子要在DIV 2混了. A,B,C都是简单题,看AC人数就知道了. A:如果我们定义数组为N*N的话就不用考虑边界了 #include<iostream> #include &l ...

随机推荐

  1. 九度oj 1530 最长不重复子串

    原题链接:http://ac.jobdu.com/problem.php?pid=1530 字符串简单题,看似O(n^2)的复杂度10000的数据量会tle,其实最长不重复子串不超过26个嘛... 如 ...

  2. cocos2dx中的CCLayerColor

    颜色图层在游戏中主要用来烘托背景,可以按照RGB设置填充颜色,同时还可以设置图层的透明度(opacity),常用于显示背景 颜色图层还存在一个特殊的子类:CCLayerGradient,是具有颜色渐变 ...

  3. 四则运算小程序测试--c++--软件工程课

    一.测试内容: 1.生成题目数是否准确?2.打印方式(列数l.行间距jj)是否准确?3.有无乘除法cc是否准确?4.数的范围fw是否准确?5.除法有无余数c是否准确?6.加减有无负数f是否准确? 二. ...

  4. dd面试经历

     HR面:看了我的简历,说fe做的简历就是不一样哈哈好吧,然后随便问了点项目,又问了什么时候可以去实习,就没了.三面:基本数据结构.冒泡排序.数组去重.ie与主流浏览器事件绑定.垂直居中的css实现方 ...

  5. ORM 框架

    1.Dapper 2.Entity Framework(EF) http://www.cnblogs.com/n-pei/archive/2011/09/06/2168433.html

  6. Careercup - Google面试题 - 5377673471721472

    2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...

  7. Tomcat配置HTTPS方式生成安全证书

    在Tomcat 6中配置SSL双向认证是相当容易的,本文将介绍如何使用JDK的keytool来为Tomcat配置双向SSL认证.并实现批量生成证书 系统需求:JDK 5.0Tomcat 6.0.16启 ...

  8. 【Maximum Depth of Binary Tree 】cpp

    题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

  9. mailx 乱码,sendmail发html邮件,脚本开机启动

    echo "yes"|mailx -s "我" -S ttycharset=utf-8 -S sendcharsets=utf-8 -S encoding=ut ...

  10. 802.11 wireless 五

    802.11 wireless 5CSMA/CA,采用倒计时的方法,退避的时间(当年时间+duration 为发送时间,每一个帧会有一个duration,这个位叫做duration[n.持续]) PS ...