fork download
  1. #include <bits/stdc++.h>
  2. #define FAST ios_base::sync_with_stdio(false);cin.tie();cout.tie();
  3. #define FILE_READ_IN freopen("input.txt","r",stdin);
  4. #define FILE_READ_OUT freopen("output.txt","w",stdout);
  5. using namespace std;
  6. typedef long long ll;
  7. vector<vector<int>> adj;
  8. vector<bool> vis;
  9. bool ans=true;
  10. ll dfs(int v,ll *p,ll *h,ll *B,int parent=-1){
  11. vis[v]=true;
  12. ll sum=0,total=p[v];
  13. for(int u:adj[v]){
  14. if(!vis[u]){
  15. total+=dfs(u,p,h,B,v);
  16. sum+=B[u];
  17. }
  18. }
  19. if((total-h[v])%2!=0) ans=false;
  20. B[v]=(total-h[v])/2;
  21. if(B[v]<0||B[v]>total) ans=false;
  22. if(sum<B[v]) ans=false;
  23.  
  24. return total;
  25. }
  26.  
  27. int main()
  28. {
  29. int t; cin>>t;
  30. while(t--){
  31. ans=false;
  32. int n; cin>>n;
  33. adj.resize(n); vis.resize(n,false);
  34. ll m; cin>>m;
  35. ll p[n],h[n];
  36. for (int i = 0; i < n; i++)
  37. {
  38. cin>>p[i];
  39. }
  40. for (int i = 0; i < n; i++)
  41. {
  42. cin>>h[i];
  43. }
  44. for(int i=0;i<n-1;i++){
  45. int u,v; cin>>u>>v; u--,v--;
  46. adj[u].push_back(v);
  47. adj[v].push_back(u);
  48. }
  49. ll B[n];
  50. dfs(0,p,h,B);
  51. if(ans) cout<<"YES\n";
  52. else cout<<"NO\n";
  53. adj.clear();
  54. vis.clear();
  55. }
  56. }
  57.  
  58.  
  59.  
Runtime error #stdin #stdout 0s 4288KB
stdin
Standard input is empty
stdout
Standard output is empty