-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC.cpp
53 lines (52 loc) · 1.07 KB
/
C.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
vector <int> g[maxn];
int p[maxn];
int h[maxn];
long long sub[maxn], good[maxn], bad[maxn];
bool ans;
void dfs(int x, int par) {
sub[x] = p[x];
for(int i : g[x]) {
if(i != par) {
dfs(i, x);
sub[x] += sub[i];
}
}
if(abs(h[x] + sub[x]) % 2 == 1) ans = false;
good[x] = (sub[x] + h[x]) / 2;
bad[x] = (sub[x] - h[x]) / 2;
if(bad[x] < 0 || good[x] < 0) ans = false;
long long total = 0;
for(int i : g[x]) {
if(i != par) {
total += bad[i];
}
}
if(total < bad[x] - p[x]) {
ans = false;
}
}
int main() {
int t;
scanf("%d", &t);
while(t--) {
int n, m;
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i++) scanf("%d", &p[i]);
for(int i = 1; i <= n; i++) scanf("%d", &h[i]);
for(int i = 1; i <= n; i++) g[i].clear();
for(int i = 1; i < n; i++) {
int x, y;
scanf("%d %d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
ans = true;
dfs(1, 0);
if(ans) puts("YES");
else puts("NO");
}
return 0;
}