提交时间:2022-08-18 11:02:12
运行 ID: 55443
#include<bits/stdc++.h> #define N 100005 using namespace std; 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<<1)+(x<<3)+(ch^48),ch=getchar();} return x*f; } int T,n,m,p[N],sum[N],h[N]; vector <int> t[N]; bool dfs(int k,int f){ sum[k]=p[k]; for(int i(0);i<t[k].size();++i){ int s(t[k][i]); if(s^f){ if(!dfs(s,k)) return 0; sum[k]+=sum[s]; } } return sum[k]>=h[k]&&min(sum[k]+h[k],sum[k]-h[k])>=0&&!(sum[k]-h[k]&1); } int main(){ // freopen("Home.in","r",stdin); // freopen("Home.out","w",stdout); T=read(); while(T--){ n=read();m=read(); for(int i(1);i<=n;++i) p[i]=read(); for(int i(1);i<=n;++i) h[i]=read(); for(int i(1);i<n;++i){ int u(read()),v(read()); t[u].push_back(v); t[v].push_back(u); } if(!dfs(1,0)) puts("NO"); else puts("YES"); } return 0; }