Codeforces Round #263 (Div. 2) D. Appleman and Tree(树形DP)
D. Appleman and Tree
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).
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 a single integer — the number of ways to split the tree modulo 1000000007 (109 + 7).
3
0 0
0 1 1
2
6
0 1 1 0 4
1 1 0 0 1 0
1
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 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点的部分会出现不含黑点的情况)
#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)的更多相关文章
- Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】
题意:给了一棵树以及每个节点的颜色,1代表黑,0代表白,求将这棵树拆成k棵树,使得每棵树恰好有一个黑色节点的方法数 解法:树形DP问题.定义: dp[u][0]表示以u为根的子树对父亲的贡献为0 dp ...
- 贪心 Codeforces Round #263 (Div. 2) C. Appleman and Toastman
题目传送门 /* 贪心:每次把一个丢掉,选择最小的.累加求和,重复n-1次 */ /************************************************ Author :R ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #382 (Div. 2) 继续python作死 含树形DP
A - Ostap and Grasshopper zz题能不能跳到 每次只能跳K步 不能跳到# 问能不能T-G 随便跳跳就可以了 第一次居然跳越界0.0 傻子哦 WA1 n,k = map ...
- 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 ...
- 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 ...
- Codeforces Round #263 (Div. 2)
吐槽:一辈子要在DIV 2混了. A,B,C都是简单题,看AC人数就知道了. A:如果我们定义数组为N*N的话就不用考虑边界了 #include<iostream> #include &l ...
随机推荐
- [原创]EnterpriseDB测试key申请方法
各位有对EnterpriseDB感兴趣的朋友,可以通过邮件方式申请测试key: 发送邮件至:zws@focus-soft.com,官方收到邮件后会有专人与您联系,一般情况都会很快得到一个测试key. ...
- Fragment+RadioButton实现点击切换页面效果
首先我们需要在主布局文件中 放一个 容器,方便让fragment加入进去,我们创建了四个Fragment,并用RedioButton实现了导航栏 MainActivity.java package c ...
- hdu 1026 Ignatius and the Princess I
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...
- golang构造函数与转换函数
golang的每种类型, 凡是用type定义的类型, 其类型名既是其构造函数,也是其转换函数. 其中,构造函数后眼{...}, 转换函数后跟(...)
- wifi-sdio接口
1.sdio接口层解析 SDIO总线 SDIO总线和USB总线类似,SDIO也有两端,其中一端是HOST端,另一端是device端.所有的通信都是由HOST端发送命令开始的,Device端只要能解析命 ...
- win10里安装.net3.5
在CMD窗口里面输入这样一段,来安装Microsoft.MET Framework 3.5 Dism /online /enable-feature /featurename:NetFX3 /All ...
- Entity Framework学习笔记(三)----CRUD(2)
请注明转载地址:http://www.cnblogs.com/arhat 昨天晚上老魏配的机器终于到了,可是拿回来之后什么都组装好了,唯独差一个非常重要的组件"电源线",老魏那个汗 ...
- 32.DDR2仿真结果
在STG之前,做了下Modelim,可以进行读写测试,关于速度的研究还需要看手册 数据终于出来了
- Python实现kNN(k邻近算法)
Python实现kNN(k邻近算法) 运行环境 Pyhton3 numpy科学计算模块 计算过程 st=>start: 开始 op1=>operation: 读入数据 op2=>op ...
- UIToolbar swift
// // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...