C. Uncle Bogdan and Country Happiness
纯纯DFS
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 3;
#define int long long
int a[N], b[N], c[N];
vector<int> cun[N];
int vis[N];
int ff = 1;
void DFS(int x)
{int res = 0;int xx = a[x];for (int i = 0; i < cun[x].size(); i++){int u = cun[x][i];if (!vis[u]){vis[u] = 1;DFS(u);a[x] += a[u]; //总人数是加上x城市的res += b[u]; //应该的幸福数值没加上x城市的}}if ((a[x]+b[x])&1) ff = 0;//如果不是偶数int cou = (a[x]-b[x])/2;if (b[x]) cou+=b[x];int cou1 = (a[x]-xx-res)/2;if (res) cou1+=res;if (cou<cou1) ff = 0;//好心情的人比子节点还少if (abs(b[x])>a[x]) ff = 0;//这个点的幸福数值大于总人数if (a[x]-xx>abs(res));//如果所有人都是坏人或者好人都不行
}
signed main()
{int t;cin >> t;while (t--){int n, m, x, y;cin >> n >> m;for (int i = 1; i <= n; i++)cun[i].clear(), vis[i] = 0;for (int i = 1; i <= n; i++)cin >> a[i]; //人数for (int i = 1; i <= n; i++)cin >> b[i];for (int i = 1; i < n; i++){cin >> x >> y;cun[x].push_back(y);cun[y].push_back(x);}ff = 1;vis[1] = 1; DFS(1);if (ff) cout << "YES" << endl;else cout << "NO" << endl;}
}