# [Codeforces Round #660 (Div. 2)] C. Uncle Bogdan and Country Happiness

#### 题解

{ g o o d [ i ] − b a d [ i ] = h [ i ] , g o o d [ i ] + b a d [ i ] = s u m [ i ] {\begin{cases} & {good[i]-bad[i]=h[i]},\\ & {good[i]+bad[i]=sum[i]} \end{cases} }

{ g o o d [ i ] = ( h [ i ] + s u m [ i ] ) / 2 b a d [ i ] = ( s u m [ i ] − h [ i ] ) / 2 {\begin{cases} & {good[i]=(h[i]+sum[i])/2}\\ & {bad[i]=(sum[i]-h[i])/2} \end{cases} }

#### 代码

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
//extern "C"{void *__dso_handle=0;}
typedef long long ll;
typedef long double ld;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>

const double PI=acos(-1.0);
const double eps=1e-6;
const ll mod=1e9+7;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
const int maxm=100+10;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

vector<int> g[maxn];
bool flag=true;
void dfsum(int u,int f)
{
ll num=p[u];
for(int i=0;i<g[u].size();i++)
{
int v=g[u][i];
if(v==f) continue;
dfsum(v, u);
num+=sum[v];
}
sum[u]=num;
}

{
if(!flag) return ;
if((h[u]+sum[u])%2 || (sum[u]-h[u])%2)
{
flag=false;
return ;
}
good[u]=(h[u]+sum[u])/2;
{
flag=false;
return ;
}
for(int i=0;i<g[u].size();i++)
{
int v=g[u][i];
if(v==f) continue ;
}
}
void dfen(int u,int f)
{
if(!flag) return ;
for(int i=0;i<g[u].size();i++)
{
int v=g[u][i];
if(v==f) continue;
if(good[u]<good[v])
{
flag=false;
return ;
}
else good[u]-=good[v];
else
{
if(good[u]<0)
{
flag=false;
return ;
}
}
}
{
flag=false;
return ;
}
for(int i=0;i<g[u].size();i++)
{
int v=g[u][i];
if(v==f) continue;
dfen(v, u);
}
}

int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(sum, 0, sizeof(sum));
memset(good, 0, sizeof(good));
memset(g, 0, sizeof(g));
int n,m;
flag=true;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%lld",&p[i]);
for(int i=1;i<=n;i++) scanf("%lld",&h[i]);
for(int i=0;i<n-1;i++){
int u,v;
scanf("%d%d",&u,&v);
g[u].pb(v);
g[v].pb(u);
}
dfsum(1,-1);