# CF 660 C. Uncle Bogdan and Country Happiness

## 题目链接

C. Uncle Bogdan and Country Happiness

## 题目概述

1 ≤ t ≤ 10000 , 1 ≤ n ≤ 1 0 5 , 0 ≤ m ≤ 1 0 9 0 ≤ p i ≤ m , ∑ i = 1 n p i = m − 1 0 9 ≤ h i ≤ 1 0 9 , i = 1 , 2 , 3 … , n . 1\leq t \leq 10000, 1\leq n \leq 10^5, 0\leq m\leq 10^9\\ 0\leq p_i \leq m, \sum_{i=1}^{n}p_i = m\\ -10^9 \leq h_i \leq 10^9 , i=1,2,3\dots,n.

## 思路

a = g + b , h = g − b ; a = g+b, h = g-b;

## 代码

/*
* @Author: Shuo Yang
* @Date: 2020-07-30 21:47:50
* @LastEditors: Shuo Yang
* @LastEditTime: 2020-07-31 22:16:58
* @FilePath: /Code/CF/660/C.cpp
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+5;
vector<int>buf[N];
int a[N],p[N],h[N],g[N];
bool flag = true;

void dfs(int v, int ancestor = -1) {
a[v] = p[v];
int sum = 0;
for(auto to: buf[v]){
if(to == ancestor)
continue;
dfs(to, v);
a[v] += a[to];
sum += g[to];
}
g[v] = (a[v]+h[v])/2;
if((a[v]+h[v])%2 != 0)
flag = false;
if(g[v] < 0 || g[v] > a[v])
flag = false;
if( sum > g[v])
flag = false;
}

int main(int argc, const char** argv) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t;
cin>>t;
while(t--){
int n,m;
cin>>n>>m;
for(int i = 1; i <= n; ++i)
cin>>p[i];
for(int i = 1; i <= n; ++i)
cin>>h[i];
for(int i = 0; i < n-1; ++i){
int a,b;
cin>>a>>b;
buf[a].push_back(b);
buf[b].push_back(a);
}
dfs(1);
if( flag )
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
flag = true;
for(int i = 1; i <=n; ++i)
buf[i].clear();
}
return 0;
}