Submission #76083889
Source Code Expand
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define ull unsigned long long
#define inf 4e18
#define eps 1e-9
#define il inline
#define ls 2*k
#define rs 2*k+1
using namespace std;
const int N=2e5+5,M=1e7+5;
const int mod=998244353;
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
return x*f;
}
int n,val[N],a[N],siz[N],sum[N];
int jc[M],nijc[M],ans=1,lst[N];
inline int qpow(int x,int y){
int res=1;
while(y){
if(y&1) res=res*x%mod;
x=x*x%mod;
y>>=1;
}
return res%mod;
}
vector<int>E[N];
inline bool check(int x,int fa){
int f=1;
sum[x]=a[x];
siz[x]=val[x];
for(auto to : E[x]){
if(to==fa) continue;
f|=check(to,x);
sum[x]+=sum[to];
siz[x]+=siz[to];
}
if(sum[x]>siz[x]) f=0;
lst[x]=siz[x]-sum[x]+a[x];
return f;
}
inline int C(int n_,int m_){
if(m_>n_) return 0;
n_%=mod;
int res=jc[(int)1e7];
for(int i=1e7+1;i<=n_;i++)
res=res*i%mod;
if(n_<=1e7) res=jc[n_];
int ret=jc[(int)1e7];
for(int i=1e7+1;i<=n_-m_;i++)
ret=ret*i%mod;
if(n_-m_<=1e7) ret=nijc[n_-m_];
else ret=qpow(ret,mod-2)%mod;
return res*nijc[m_]%mod*ret%mod;
}
inline void dfs(int x,int fa){
for(auto to : E[x]){
if(to==fa) continue;
dfs(to,x);
}
ans=ans*C(lst[x],a[x])%mod;
// cout<<x<<' '<<lst[x]<<' '<<a[x]<<'\n';
return ;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
jc[0]=1,nijc[0]=1;
for(int i=1;i<=1e7;i++){
jc[i]=jc[i-1]*i%mod;
nijc[i]=qpow(jc[i],mod-2);
}
int tc=1;
while(tc--){
cin>>n;
for(int i=2,x;i<=n;i++){
cin>>x;
E[i].push_back(x);
E[x].push_back(i);
}
for(int i=1;i<=n;i++) cin>>val[i];
for(int i=1;i<=n;i++) cin>>a[i];
if(!check(1,0)){
cout<<0<<'\n';
exit(0);
}
dfs(1,0);
cout<<ans<<'\n';
}
return 0;
}
/*
input:
1
3 4 1
1 2 3
3 3 4 7
6
output:
14
从大到小按位考虑,则如果能在不会让前面取不到的情况下取到,那么一定要操作。
*/
Submission Info
| Submission Time | |
|---|---|
| Task | E - Select from Subtrees |
| User | Limingxuan |
| Language | C++23 (GCC 15.2.0) |
| Score | 0 |
| Code Size | 2190 Byte |
| Status | WA |
| Exec Time | > 2000 ms |
| Memory | 184488 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 | 996 ms | 159992 KiB |
| example_01.txt | AC | 997 ms | 159912 KiB |
| example_02.txt | AC | 997 ms | 159860 KiB |
| hand_00.txt | TLE | > 2000 ms | 184080 KiB |
| hand_01.txt | TLE | > 2000 ms | 179812 KiB |
| hand_02.txt | TLE | > 2000 ms | 180708 KiB |
| hand_03.txt | TLE | > 2000 ms | 183596 KiB |
| hand_04.txt | TLE | > 2000 ms | 182028 KiB |
| hand_05.txt | TLE | > 2000 ms | 181968 KiB |
| hand_06.txt | TLE | > 2000 ms | 181220 KiB |
| hand_07.txt | WA | 997 ms | 159912 KiB |
| hand_08.txt | AC | 995 ms | 159832 KiB |
| hand_09.txt | TLE | > 2000 ms | 184156 KiB |
| hand_10.txt | TLE | > 2000 ms | 159564 KiB |
| hand_11.txt | AC | 1077 ms | 184488 KiB |
| random_00.txt | AC | 996 ms | 159728 KiB |
| random_01.txt | TLE | > 2000 ms | 159460 KiB |
| random_02.txt | TLE | > 2000 ms | 159564 KiB |
| random_03.txt | AC | 998 ms | 159860 KiB |
| random_04.txt | AC | 997 ms | 159736 KiB |
| random_05.txt | TLE | > 2000 ms | 174752 KiB |
| random_06.txt | TLE | > 2000 ms | 172048 KiB |
| random_07.txt | TLE | > 2000 ms | 162020 KiB |
| random_08.txt | TLE | > 2000 ms | 174088 KiB |
| random_09.txt | TLE | > 2000 ms | 172980 KiB |
| random_10.txt | TLE | > 2000 ms | 179428 KiB |
| random_11.txt | TLE | > 2000 ms | 179560 KiB |
| random_12.txt | TLE | > 2000 ms | 179544 KiB |
| random_13.txt | TLE | > 2000 ms | 179556 KiB |
| random_14.txt | TLE | > 2000 ms | 179560 KiB |
| random_15.txt | TLE | > 2000 ms | 179568 KiB |
| random_16.txt | TLE | > 2000 ms | 179560 KiB |
| random_17.txt | TLE | > 2000 ms | 179532 KiB |
| random_18.txt | TLE | > 2000 ms | 179464 KiB |
| random_19.txt | TLE | > 2000 ms | 179532 KiB |