Submission #76081938
Source Code Expand
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = vector<ll>;
using vvi = vector<vi>;
using vc = vector<char>;
using vvc = vector<vc>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vs = vector<string>;
using pii = pair<ll, ll>;
using vpii = vector<pii>;
using vvpii = vector<vpii>;
using mint = modint998244353;
// using mint = modint1000000007;
#define endl '\n'
#define OVERLOAD_REP(a, b, c, d, name, ...) name
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define REP0(x) for (ll _rep_counter = 0; _rep_counter < (x); ++_rep_counter)
#define REP1(i, x) for (ll i = 0; (i) < (x); ++(i))
#define REP2(i, l, r) for (ll i = (l); (i) < (r); ++(i))
#define REP3(i, l, r, c) for (ll i = (l); ((c) > 0 ? (i) < (r) : (i) > (r)); i += (c))
#define all(x) (x).begin(), (x).end()
const ll INF = LLONG_MAX / 4;
vi dx = {1, 0, -1, 0, 1, 1, -1, -1};
vi dy = {0, 1, 0, -1, 1, -1, 1, -1};
vc alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
vc ALPHABET = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
const int MAX = 2e6;
mint fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
const int MOD = mint::mod();
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i;
inv[i] = MOD - inv[MOD % i] * (MOD / i);
finv[i] = finv[i - 1] * inv[i];
}
}
// 二項係数計算
mint COM(int n, int k) {
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * finv[k] * finv[n - k];
}
void io_setup()
{
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(16);
}
int main(void)
{
io_setup();
COMinit();
int n;
cin >> n;
vvi g(n);
rep(i,1,n){
int p;
cin >> p;
p--;
g[p].push_back(i);
}
vi c(n), d(n);
rep(i,n)cin >> c[i];
rep(i,n)cin >> d[i];
mint ans = 1;
vi sum(n), need(n);
bool ok = true;
auto dfs = [&](auto self, int v)->void{
sum[v] = c[v];
ll child_need=0;
for(auto to: g[v]){
self(self, to);
sum[v]+=sum[to];
child_need += need[to];
}
need[v]=child_need + d[v];
if(need[v]>sum[v])ok=false;
ll nokori = sum[v]-child_need;
ans *= COM(nokori, d[v]);
};
dfs(dfs, 0);
if(!ok)cout << 0 << endl;
else cout << ans.val() << endl;
}
Submission Info
| Submission Time |
|
| Task |
E - Select from Subtrees |
| User |
karas1 |
| Language |
C++23 (GCC 15.2.0) |
| Score |
0 |
| Code Size |
2831 Byte |
| Status |
RE |
| Exec Time |
200 ms |
| Memory |
65872 KiB |
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
0 / 450 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
example_00.txt, example_01.txt, example_02.txt |
| All |
example_00.txt, example_01.txt, example_02.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, hand_10.txt, hand_11.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt |
| Case Name |
Status |
Exec Time |
Memory |
| example_00.txt |
AC |
28 ms |
26908 KiB |
| example_01.txt |
AC |
25 ms |
26936 KiB |
| example_02.txt |
RE |
127 ms |
26584 KiB |
| hand_00.txt |
RE |
200 ms |
65792 KiB |
| hand_01.txt |
RE |
153 ms |
39320 KiB |
| hand_02.txt |
RE |
165 ms |
40664 KiB |
| hand_03.txt |
RE |
166 ms |
40816 KiB |
| hand_04.txt |
RE |
175 ms |
52380 KiB |
| hand_05.txt |
RE |
157 ms |
41504 KiB |
| hand_06.txt |
RE |
182 ms |
54716 KiB |
| hand_07.txt |
RE |
127 ms |
26648 KiB |
| hand_08.txt |
RE |
124 ms |
26736 KiB |
| hand_09.txt |
RE |
187 ms |
65752 KiB |
| hand_10.txt |
RE |
129 ms |
26756 KiB |
| hand_11.txt |
AC |
101 ms |
65872 KiB |
| random_00.txt |
AC |
26 ms |
26960 KiB |
| random_01.txt |
RE |
125 ms |
26692 KiB |
| random_02.txt |
RE |
125 ms |
26820 KiB |
| random_03.txt |
AC |
25 ms |
27016 KiB |
| random_04.txt |
AC |
25 ms |
27096 KiB |
| random_05.txt |
RE |
156 ms |
37996 KiB |
| random_06.txt |
RE |
150 ms |
35788 KiB |
| random_07.txt |
RE |
128 ms |
28612 KiB |
| random_08.txt |
RE |
154 ms |
37292 KiB |
| random_09.txt |
RE |
153 ms |
36436 KiB |
| random_10.txt |
RE |
176 ms |
41456 KiB |
| random_11.txt |
RE |
169 ms |
41284 KiB |
| random_12.txt |
RE |
168 ms |
41300 KiB |
| random_13.txt |
RE |
166 ms |
41452 KiB |
| random_14.txt |
RE |
165 ms |
41284 KiB |
| random_15.txt |
RE |
165 ms |
41336 KiB |
| random_16.txt |
RE |
168 ms |
41296 KiB |
| random_17.txt |
RE |
163 ms |
41300 KiB |
| random_18.txt |
RE |
165 ms |
41452 KiB |
| random_19.txt |
RE |
165 ms |
41456 KiB |