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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| #include <cstdio> #include <algorithm> #include <iostream> #include <vector> #include <map> #include <queue> #include <set> #include <ctime> #include <cstring> #include <cstdlib> #include <math.h> using namespace std; typedef long long ll; const ll N = 1e3 + 5; const ll maxn = 3e5 + 5; //ll a[maxn]; char s[N]; ll cnt; map<ll, ll> mp; ll pre[maxn], sum[maxn], head[maxn], w[maxn], nums[maxn]; ll good[maxn]; struct node { ll v, nex; } edge[maxn]; void add(ll u, ll v) { edge[cnt].v = v, edge[cnt].nex = head[u]; head[u] = cnt++; } bool fla; void dfs(ll u, ll fa) { pre[u] = fa; for (ll i = head[u]; ~i; i = edge[i].nex) { ll v = edge[i].v; if (v != fa) dfs(v, u), nums[u] += nums[v]; } ll x = (nums[u] + w[u]) / 2, y; y = nums[u] - x; if (x < 0 || y < 0 || x + y != nums[u] || x - y != w[u]) fla = false; good[u] = x; ll sum = 0; for (int i = head[u]; ~i; i = edge[i].nex) sum += good[edge[i].v]; if (sum > good[u]) fla = false; } int main() { ios::sync_with_stdio(false); cin.tie(0); ll t; cin >> t; while (t--) { memset(head, -1, sizeof head); ll n, m; cnt = 0; cin >> n >> m; for (ll i = 1; i <= n; i++) cin >> nums[i], good[i] = 0; for (ll i = 1; i <= n; i++) cin >> w[i]; ll u, v; for (ll i = 1; i < n; i++) cin >> u >> v, add(u, v), add(v, u); fla = true; dfs(1, 0); if (fla) cout << "YES" << endl; else cout << "NO" << endl; } }
|