提交时间:2022-08-18 10:57:42
运行 ID: 55408
#include <bits/stdc++.h> using namespace std; int p[100005],h[100005]; vector<int> ed[100005]; bool a[100005]; int b[100005],K[100005]; void dfs(int x){ int i; a[x]=1; b[x]=p[x]; for(i=0;i<ed[x].size();i++){ if(!a[ed[x][i]]){ dfs(ed[x][i]); b[x]+=b[ed[x][i]]; } } } bool dfs1(int x){ int i,ret=0; bool yes=1; a[x]=1; for(i=0;i<ed[x].size();i++){ if(!a[ed[x][i]]){ yes&=dfs1(ed[x][i]); ret+=K[ed[x][i]]; } } return yes&(ret>=K[x]-p[x]); } int main(){ // freopen("home.in","r",stdin); // freopen("home.out","w",stdout); int t,k=0; scanf("%d",&t); while(t--) { int n,m,i; for(i=1;i<=k;i++) ed[i].clear(),a[i]=b[i]=0; bool no=0; scanf("%d%d",&n,&m); k=n; for(i=1;i<=n;i++) scanf("%d",&p[i]); for(i=1;i<=n;i++) scanf("%d",&h[i]); for(i=1;i<n;i++){ int x,y; scanf("%d%d",&x,&y); ed[x].push_back(y); ed[y].push_back(x); } dfs(1); for(i=1;i<=n;i++) K[i]=(b[i]-h[i])/2; for(i=1;i<=n;i++){ if(abs(h[i])>b[i]||(b[i]-h[i])%2){ puts("NO"); no=1; break; } } for(i=1;i<=n;i++) a[i]=0; if(!no) printf("%s\n",dfs1(1)?"YES":"NO"); } return 0; }