Codeforces 461B Appleman and Tree
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的更多相关文章
- Codeforces 461B Appleman and Tree(木dp)
题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...
- Codeforces 461B. Appleman and Tree[树形DP 方案数]
B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 461B Appleman and Tree:Tree dp
题目链接:http://codeforces.com/problemset/problem/461/B 题意: 给你一棵树(编号从0到n-1,0为根节点),每个节点有黑白两种颜色,其中黑色节点有k+1 ...
- Codeforces 461B - Appleman and Tree 树状DP
一棵树上有K个黑色节点,剩余节点都为白色,将其划分成K个子树,使得每棵树上都仅仅有1个黑色节点,共同拥有多少种划分方案. 个人感觉这题比較难. 如果dp(i,0..1)代表的是以i为根节点的子树种有0 ...
- 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 ...
- CodeForces 461B Appleman and T
题目链接:http://codeforces.com/contest/461/problem/B 题目大意: 给定一课树,树上的节点有黑的也有白的,有这样一种分割树的方案,分割后每个子图只含有一个黑色 ...
- 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 ...
- 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 ...
- CodeForces 416 B Appleman and Tree DP
Appleman and Tree 题解: 定义dp[u][1] 为以u的子树范围内,u这个点已经和某个黑点相连的方案数. dp[u][0] 为在u的子树范围内, u这个点还未和某个黑点相连的方案数. ...
随机推荐
- Android中程序包的相关操作
//获取系统中已经安装的应用程序 List<PackageInfo> packageinfos=this.getPackageManager().getInstalledPackages( ...
- memory_target not supported on this system
- P - Atlantis - hdu1542(求面积)
题意:rt 求面积......不计算重复面积(废话..)hdu1255 的弱化版,应该先做这道题在做那道题的. ******************************************** ...
- Java毫秒转换成日期格式
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.uti ...
- Java中setCharAt()方法介绍
--转载自网络,备忘 这是StringBuffer类里面的一个方法:主要是用来替换的,方法里面有两个参数setCharAt(int index,Char ch),第一个参数是取代的位置 索引从0开始 ...
- JS瀑布流效果
本篇内容实现类似百度图片的呈现功能,瀑布流+自动加载 index13.html <!DOCTYPE html> <html> <head> <meta cha ...
- writev/readv
```cpp#include <sys/uio.h>ssize_t readv(int fd, const struct iovec *iov, int iovcnt);ssize_t w ...
- (转)重置Mac OS X管理员密码
忘记Mac管理员密码怎么办?别担心,办法总会有的. [方法一] 开机按住option,选择Recovery HD(Snow Leopard插入光盘开机按住C) Snow Leopard系统:进入后在上 ...
- 苹果有益让老iPhone变慢以迫使消费者购买新一代的iPhone?
首先,来一组来自谷歌Trends的图片.(谷歌Trends记录了某段时间内相关关键词搜索的次数.) 假设你做数据,那么你应该会有些感觉. 特别是第一幅图,它规律似乎比第二幅更明显,第二幅图仅仅是一个普 ...
- [ES6] Class Inherit
In constructor, you can call parent's constuctor() method by supert(); class ShoppingCart { construc ...