General
 
 
# Author Problem Lang Verdict Time Memory Sent Judged  
127829679 Practice:
Czhgugugu
461B - 15 C++17 (GCC 9-64) Accepted 31 ms 15356 KB 2021-09-04 12:52:19 2021-09-04 12:52:19
→ Source
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair <int, int> pin;

#ifndef ONLINE_JUDGE
bool MEMORY_ST;
#endif

const int N = 1e5 + 5;
const ll P = 1e9 + 7;

int n, tot, head[N], fa[N], c[N];
ll f[N][2];

struct Edge {
    int to, nxt;
} e[N];

inline void add(int from, int to) {
    e[++tot].to = to;
    e[tot].nxt = head[from];
    head[from] = tot;
}

inline void inc(ll &x, ll y) {
    x += y;
    if (x >= P) x -= P;
}

inline void sub(ll &x, ll y) {
    x -= y;
    if (x < 0) x += P;
}

namespace Fread {
    const int L = 1 << 15;
    
    char buffer[L], *S, *T;
    
    inline char Getchar() {
        if(S == T) {
            T = (S = buffer) + fread(buffer, 1, L, stdin);
            if(S == T) return EOF;
        }
        return *S++;
    }
    
    template <class T> 
    inline void read(T &X) {
        char ch; T op = 1;
        for(ch = Getchar(); ch > '9' || ch < '0'; ch = Getchar())
            if(ch == '-') op = -1;
        for(X = 0; ch >= '0' && ch <= '9'; ch = Getchar()) 
            X = (X << 1) + (X << 3) + ch - '0'; 
        X *= op;
    }
    
} using Fread::read;   

void dfs(int x) {
    f[x][c[x]] = 1;
    for (int i = head[x]; i; i = e[i].nxt) {
        int y = e[i].to;
        dfs(y);
        ll f0 = f[x][0], f1 = f[x][1];
        f[x][0] = f[x][1] = 0;
        inc(f[x][1], f[y][1] * f0 % P);
        inc(f[x][1], f[y][0] * f1 % P);
        inc(f[x][1], f[y][1] * f1 % P);
        inc(f[x][0], f[y][0] * f0 % P);
        inc(f[x][0], f[y][1] * f0 % P);
    } 
}

#ifndef ONLINE_JUDGE
bool MEMORY_ED;
#endif

int main() {
#ifndef ONLINE_JUDGE
    freopen("sample.in", "r", stdin);
    clock_t st_clock = clock();
#endif

    read(n);
    for (int i = 2; i <= n; i++) {
        read(fa[i]);
        ++fa[i];
        add(fa[i], i);
    }
    for (int i = 1; i <= n; i++) read(c[i]);
    dfs(1);
    printf("%lld\n", f[1][1]);

    // for (int i = 1; i <= n; i++)
    //     printf("%lld %lld\n", f[i][0], f[i][1]); 

#ifndef ONLINE_JUDGE
    clock_t ed_clock = clock();
    printf("time = %f ms\n", (double)(ed_clock - st_clock) / CLOCKS_PER_SEC * 1000);
    printf("memory = %.2f MB\n", (&MEMORY_ED - &MEMORY_ST) / 1024.0 / 1024.0);
#endif
    return 0;
}
?
Time: ? ms, memory: ? KB
Verdict: ?
Input
?
Participant's output
?
Jury's answer
?
Checker comment
?
Diagnostics
?
Click to see test details