提交时间:2024-01-09 09:17:47

运行 ID: 24624

#include <bits/stdc++.h> #define int long long #define double long double #define endl "\n" #define pii pair<double,int> #define p1(x) ((x).first) #define p2(x) ((x).second) #define ls(x) T[x].ls #define rs(x) T[x].rs #define ds(x) T[x].ds #define w(x) T[x].e.w #define t(x) T[x].e.v #define i128 __int128_t using namespace std; int n; const int M=998244353; inline int qp(int a,int x){ int res=1; while(x){ if(x&1)res=res*a%M; x>>=1; a=a*a%M; } return res; } inline int inv(int x){return qp(x,M-2);} struct doly{ int a,b; inline doly IT(){ return {a*inv(b+1)%M,(b+1)%M}; } inline int f(int x){ return a*qp(x,b)%M; } friend doly operator *(const doly A,const doly B){ return {(A.a*B.a)%M,(A.b+B.b)%M}; } friend doly operator *(const doly A,const int B){ return {(A.a*B)%M,A.b}; } }T[200300]; int p[200300],q[200300]; vector<int>g[200300]; inline void dfs(int u,int fa){ T[u]={1,0}; for(int v:g[u]) if(v!=fa)dfs(v,u),T[u]=T[u]*T[v]; if(p[u]==-3) T[u].b=T[u].b*inv(q[u])%M; if(p[u]==-1) T[u].a=T[u].a*inv(qp(q[u],T[u].b))%M; if(p[u]==0) T[u]=T[u].IT()*inv(q[u]); } signed main(){ ios::sync_with_stdio(0); // freopen("P2483_3.in","r",stdin); // freopen("b.out","w",stdout); int t; cin>>t; while(t--){ cin>>n; bool e=0; for(int i=1;i<=n;i++) cin>>p[i]>>q[i],e|=p[i]>=1,g[i].clear(); for(int i=1;i<n;i++){ int u,v; cin>>u>>v; g[u].push_back(v); g[v].push_back(u); } if(e)cout<<1<<endl; else{ dfs(1,0); cout<<(1-T[1].IT().f(1)+M)%M<<endl; } } cout.flush(); return 0; }