http://codeforces.com/problemset/problem/461/B

思路:dp,dp[i][0]代表这个联通块没有黑点的方案数,dp[i][1]代表有一个黑点的方案数

转移:
首先如果儿子节点是个有黑点的,父亲节点也有黑点,那么只能分裂开,不能合并在一起,只对dp[u][1]有贡献

如果儿子节点是个有黑点的,父亲节点没有黑点,那么可以分裂也可以合并,对dp[u][1]和dp[u][0]都有贡献

如果儿子节点是个没有黑点的,父亲节点有黑点,那么必须和父亲合并,对dp[u][1]有贡献

如果儿子节点是个没有黑点的,父亲节点也没黑点,那么必须和父亲合并,对dp[u][0]有贡献

 #include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
#define ll long long
const ll Mod=;
ll f[][];
int v[],n,tot,go[],first[],next[];
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void insert(int x,int y){
tot++;
go[tot]=y;
next[tot]=first[x];
first[x]=tot;
}
void add(int x,int y){
insert(x,y);insert(y,x);
}
void dfs(int x,int fa){
if (v[x]==) f[x][]=,f[x][]=;else f[x][]=,f[x][]=;
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (pur==fa) continue;
dfs(pur,x);
ll t0=f[x][],t1=f[x][];
f[x][]=((t0*f[pur][])%Mod+(t1*f[pur][])%Mod)+(t1*f[pur][])%Mod;
f[x][]=((t0*f[pur][])%Mod+(t0*f[pur][])%Mod);
}
}
int main(){
n=read();
for (int i=;i<=n;i++){
int x=read()+;
add(x,i);
}
for (int i=;i<=n;i++) v[i]=read();
dfs(,);
printf("%I64d\n",(f[][])%Mod);
return ;
}

Codeforces 461B Appleman and Tree的更多相关文章

  1. Codeforces 461B Appleman and Tree(木dp)

    题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...

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

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

  3. Codeforces 461B Appleman and Tree:Tree dp

    题目链接:http://codeforces.com/problemset/problem/461/B 题意: 给你一棵树(编号从0到n-1,0为根节点),每个节点有黑白两种颜色,其中黑色节点有k+1 ...

  4. Codeforces 461B - Appleman and Tree 树状DP

    一棵树上有K个黑色节点,剩余节点都为白色,将其划分成K个子树,使得每棵树上都仅仅有1个黑色节点,共同拥有多少种划分方案. 个人感觉这题比較难. 如果dp(i,0..1)代表的是以i为根节点的子树种有0 ...

  5. 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 ...

  6. CodeForces 461B Appleman and T

    题目链接:http://codeforces.com/contest/461/problem/B 题目大意: 给定一课树,树上的节点有黑的也有白的,有这样一种分割树的方案,分割后每个子图只含有一个黑色 ...

  7. 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 ...

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

    题目链接 D. Appleman and Tree time limit per test :2 seconds memory limit per test: 256 megabytes input ...

  9. CodeForces 416 B Appleman and Tree DP

    Appleman and Tree 题解: 定义dp[u][1] 为以u的子树范围内,u这个点已经和某个黑点相连的方案数. dp[u][0] 为在u的子树范围内, u这个点还未和某个黑点相连的方案数. ...

随机推荐

  1. 【HDOJ】1104 Remainder

    bfs. #include <cstdio> #include <cstring> #include <cstdlib> #include <queue> ...

  2. B450配置

    电脑型号 联想 B450 笔记本电脑  (扫描时间:2015?5?7?操作系统 Windows 7 旗舰版 32位 SP1 ( DirectX 11 ) 处理器 英特尔 酷睿2 双核 T9800 @ ...

  3. Linux系统编程(32)—— socket编程之TCP服务器与客户端

    TCP协议的客户端/服务器程序的一般流程 服务器调用socket().bind().listen()完成初始化后,调用accept()阻塞等待,处于监听端口的状态,客户端调用socket()初始化后, ...

  4. 【转】Linux 中断学习之小试牛刀篇

    原文网址:http://www.linuxidc.com/Linux/2011-02/32129.htm 前言 在前面分析了中断的基本原理后,就可以写一个内核中断程序来体验以下,也可以借此程序继续深入 ...

  5. delphi编写dll心得, 谢谢原作者的分享。转

    delphi编写dll心得 1.每个函数体(包括exports和非exports函数)后面加 'stdcall;', 以编写出通用的dll2.exports函数后面必须加'export;'(放在'st ...

  6. 【疑难杂症】xmind启动后,自动退出的问题

    xmind安装一段时间后,就会出现一启动,就自动退出的情况.卸载重装也无法解决,在试过网上的各种方法后,发现这个方法最凑效. 打开xmind.ini(安装目录下),删除以下几行,保存配置文件,重启即可 ...

  7. MongoDB的数据类型

    最近在写一个lua的MongoDB模块.MongoDB版本3.2,lua则是5.3.1.底层以C++来写,再把函数暴露给lua调用.但是在lua中打印结果时,发现了些奇怪的现象.首先,数据库中的内容: ...

  8. 转:Java生成图片验证码(有点仿QQ验证码的意思)

    http://blog.csdn.net/ruixue0117/article/details/22829557 java: VerifyCodeUtils.java package com.fro. ...

  9. 黑马程序员_<<泛型>>

    --------------------ASP.Net+Android+IOS开发..Net培训.期待与您交流! -------------------- 1.     泛型 1.概述 泛型是为了解决 ...

  10. IOS开发之微博的设计与实现

    // // main.m // Microblog // #import <Foundation/Foundation.h> #import "Person.h" #i ...