11102022-03-03 22:26:38peti1234Fájlrendszercpp14Accepted 40/4046ms16996 KiB
#include <bits/stdc++.h>

using namespace std;
const int c=100005;
int n;
vector<int> sz[c];
long long db[c], valasz=1, mod=1e9+7;
pair<long long, long long> dfs(int a) {
    long long cnt=0, fh=0;
    for (auto x:sz[a]) {
        pair<long long, long long> p=dfs(x);
        cnt+=p.first, fh+=p.second;
    }
    if (sz[a].size()==0) {
        fh++;
    }
    if (db[a]!=-1) {
        long long s=db[a]-cnt;
        if (s<0 || s>0 && fh==0) valasz=0;
        else {
            if (fh==2) {
                valasz=valasz*(s+1)%mod;
            }
        }
        return {db[a], 0};
    } else {
        return {cnt, fh};
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin >> n;
    for (int i=1; i<=n; i++) {
        int x;
        cin >> x >> db[i];
        if (i>1) {
            sz[x].push_back(i);
        }
    }
    dfs(1);
    cout << valasz << "\n";
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/04ms6592 KiB
2Accepted0/023ms9996 KiB
3Accepted1/13ms7148 KiB
4Accepted1/13ms7152 KiB
5Accepted1/13ms7164 KiB
6Accepted1/13ms7168 KiB
7Accepted2/23ms7172 KiB
8Accepted2/24ms7392 KiB
9Accepted3/34ms7452 KiB
10Accepted3/34ms7652 KiB
11Accepted3/36ms7832 KiB
12Accepted3/36ms8036 KiB
13Accepted4/413ms9488 KiB
14Accepted4/417ms10620 KiB
15Accepted4/435ms11748 KiB
16Accepted4/446ms15800 KiB
17Accepted4/446ms16996 KiB