CF461B Appleman and Tree
#include<cstdio> #define ll long long #include<algorithm> using namespace std; const int N=1e5+10; const ll mod=1e9+7; int v[N],h[N],n,p,cnt; ll dp[N][2]; struct edge{ int v,nxt; }e[N<<1]; inline void add(int a,int b) { e[++cnt].v=b; e[cnt].nxt=h[a]; h[a]=cnt; } template <typename e> inline void read(e &x) { x=0;int f=1;char ch=getchar(); while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9') {x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} x*=f; } void dfs(int u,int fa) { for(int i=h[u];i;i=e[i].nxt) { int v=e[i].v; if(v==fa) continue; dfs(v,u); dp[u][1]=(dp[u][1]*((dp[v][0]+dp[v][1])%mod)%mod+dp[u][0]*dp[v][1]%mod)%mod; dp[u][0]=dp[u][0]*(dp[v][0]+dp[v][1]%mod)%mod; } } int main() { read(n);register int i; for(i=2;i<=n;++i) read(p),++p,add(p,i),add(i,p); for(i=1;i<=n;++i) { read(v[i]); dp[i][v[i]]=1; } dfs(1,0); printf("%lld",dp[1][1]); return 0; }