I found an interesting problem on https://codeforces.com/problemset/problem/1388/C:
Uncle Bogdan is in captain Flint's crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index.

There are $n$ cities and $n−1$ undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Cities are numbered from $1$ to $n$ and the city $1$ is a capital. In other words, the country has a tree structure.

There are $m$ citizens living in the country. A $p_i$ people live in the $i$-th city but all of them are working in the capital. At evening all citizens return to their home cities using the shortest paths. 

Every person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood. Moreover any person can ruin his mood on the way to the hometown. If person is in bad mood he won't improve it.

Happiness detectors are installed in each city to monitor the happiness of each person who visits the city. The detector in the $i$-th city calculates a happiness index $h_i$ as the number of people in good mood minus the number of people in bad mood. Let's say for the simplicity that mood of a person doesn't change inside the city.

Happiness detector is still in development, so there is a probability of a mistake in judging a person's happiness. One late evening, when all citizens successfully returned home, the government asked uncle Bogdan (the best programmer of the country) to check the correctness of the collected happiness indexes.

Uncle Bogdan successfully solved the problem. Can you do the same?

More formally, You need to check: "Is it possible that, after all people return home, for each city $i$ the happiness index will be equal exactly to $h_i$".


-----Input-----

The first line contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases.

The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 10^5$; $0 \le m \le 10^9$) — the number of cities and citizens.

The second line of each test case...

I tried it in Python, but could not do it. Can you solve it?
from sys import stdin
import sys
sys.setrecursionlimit(300000)

def dfs(v,pa):

    good = 0
    bad = 0
    for nex in lis[v]:
        if nex != pa:
            nans,ng,nb = dfs(nex,v)
            if not nans:
                return nans,0,0
            good += ng
            bad  += nb

    num = good + bad + p[v]
    if (num - h[v]) % 2 == 0:
        newbad  = (num - h[v])//2
    else:
        return False,0,0
    newgood = num - newbad

    if newbad - p[v] > bad or newgood < good or newbad < 0 or newgood < 0:
        return False,0,0
    else:
        return True,newgood,newbad




tt = int(stdin.readline())

for loop in range(tt):

    n,m = list(map(int,stdin.readline().split()))
    p = list(map(int,stdin.readline().split()))
    h = list(map(int,stdin.readline().split()))

    lis = [ [] for i in range(n)]
    for i in range(n-1):
        v,u = list(map(int,stdin.readline().split()))
        v -= 1
        u -= 1
        lis[v].append(u)
        lis[u].append(v)

    ans,good,bad = dfs(0,0)
    
    if ans:
        print ("YES")
    else:
        print ("NO")