Codeforces 461B Appleman and Tree

时间:2023-03-09 23:10:53
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 ;
}