Codeforces Round #263 (Div. 2) D. Appleman and Tree(树形DP)

时间:2023-01-11 00:19:30

题目链接

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
3
0 0
0 1 1
output
2
input
6
0 1 1 0 4
1 1 0 0 1 0
output
1
input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
output
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点的部分会出现不含黑点的情况)

 #include <stdio.h>
#include <string.h>
#include <iostream>
#define mod 1000000007 using namespace std ; struct node
{
int u ;
int v ;
int next ;
}p[];
int cnt,head[],color[] ;
long long dp[][] ; void addedge(int u,int v)
{
p[cnt].u = u ;
p[cnt].v = v ;
p[cnt].next = head[u] ;
head[u] = cnt ++ ;
}
void DFS(int u)
{
dp[u][color[u]] = ;
for(int i = head[u] ; i+ ; i = p[i].next)
{
int v = p[i].v ;
DFS(v) ;
dp[u][] = ((dp[u][] * dp[v][]) % mod + (dp[u][] * dp[v][]) % mod + (dp[u][] * dp[v][]) % mod) % mod ;
dp[u][] = ((dp[u][] * dp[v][]) % mod + (dp[u][] * dp[v][]) % mod) % mod ;
}
}
int main()
{
int n ,a;
while(~scanf("%d",&n))
{
cnt = ;
memset(head,-,sizeof(head)) ;
memset(dp,,sizeof(dp)) ;
for(int i = ; i < n ; i++)
{
scanf("%d",&a) ;
addedge(a,i) ;
}
for(int i = ; i < n ; i++)
scanf("%d",&color[i]) ;
DFS() ;
printf("%I64d\n",dp[][]) ;
}
return ;
}

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

  1. Codeforces Round &num;263 Div&period;1 B Appleman and Tree --树形DP【转】

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

  2. 贪心 Codeforces Round &num;263 &lpar;Div&period; 2&rpar; C&period; Appleman and Toastman

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

  3. Codeforces Round &num;263 &lpar;Div&period; 1&rpar; C&period; 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 &num;263 &lpar;Div&period; 2&rpar; A&period; Appleman and Easy Task【地图型搜索&sol;判断一个点四周&OpenCurlyQuote;o’的个数的奇偶】

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

  5. Codeforces Round &num;196 &lpar;Div&period; 2&rpar; D&period; Book of Evil 树形dp

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

  6. Codeforces Round &num;382 &lpar;Div&period; 2&rpar; 继续python作死 含树形DP

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

  7. Codeforces Round &num;419 &lpar;Div&period; 1&rpar; C&period; 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 &num;267 &lpar;Div&period; 2&rpar; C&period; George and Job(DP)补题

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

  9. Codeforces Round &num;263 &lpar;Div&period; 2&rpar;

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

随机推荐

  1. java function retry wrapper

    import java.util.concurrent.Callable; /** * Created by huahui.yang on 1/29/16. */ public class Retry ...

  2. 提取KIndle中每本书的笔记并单独保存

    整体思路 目标:将Kindle中的每本书的笔记标注单独提取出保存为一个Markdown文件 其中检测KIndle是否已经正常插入的判断方法: 思路1:读取媒介挂载记录 思路2:直接判断挂载地址是否存在 ...

  3. hdu 1556&colon;Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. &lbrack;转&rsqb;jquery Fancybox丰富的弹出层效果

    本文转自:http://www.helloweba.com/view-blog-65.html Fancybox是一款优秀的jquery插件,它能够展示丰富的弹出层效果.前面我们有文章介绍了facyb ...

  5. Junit3

    package code; public class MyCode { public int m1(){ System.out.println("数字型"); return 1; ...

  6. POJ2451 Uyuw&&num;39&semi;s Concert&lpar;半平面交&rpar;

    题意就是给你很多个半平面,求半平面交出来的凸包的面积. 半平面交有O(n^2)的算法,就是每次用一个新的半平面去切已有的凸包,更新,这个写起来感觉也不是特别好写. 另外一个O(nlogn)的算法是将半 ...

  7. EF 6&period;0使用小计

    ---恢复内容开始--- 最近尝试了下EF Extended,但是居然需要EF6.0以上,没办法,只能安装了,打开解决方案,选择库程序包管理下的程序包管理控制台(或者直接右击你需要使用扩展的解决方案选 ...

  8. js阻止冒泡

    js阻止冒泡 (ev || event).cancelBubble = true; 标签切换 <script type="text/javascript"> windo ...

  9. 一段获取app性能指标的py脚本

    #coding:utf-8 import os import timeimport datetimeimport subprocess ActivityManager = 'homepage.Main ...

  10. 脱产班第五次大作业-FTP服务器

    下载项目 my_ftp #!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import hmac import json import ...