C. Uncle Bogdan and Country Happiness

C. Uncle Bogdan and Country Happiness

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
#include <cassert>
//#include <unordered_set>
//#include <unordered_map>
#define ll              long long
#define pii             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define forn(i, n)      for(int i = 0; i < int(n); i++)
using namespace std;
int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-6;
const int mod = 1e9 + 7;

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
inline ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
void exgcd(ll A, ll B, ll& x, ll& y)
{
    if (B) exgcd(B, A % B, y, x), y -= A / B * x; else x = 1, y = 0;
}
inline int qpow(int x, ll n) {
    int r = 1;
    while (n > 0) {
        if (n & 1) r = 1ll * r * x % mod;
        n >>= 1; x = 1ll * x * x % mod;
    }
    return r;
}
inline int inv(int x) {
    return qpow(x, mod - 2);
}
ll lcm(ll a, ll b)
{
    return a * b / gcd(a, b);
}
/**********************************************************/
const int N = 5e3 + 5;

int dfs(vector<vector<int>>& g, vector<int>& p, vector<int>& pass, int x,int fa)
{
    for (int i = 0; i < g[x].size(); i++)
    {
        if (g[x][i] != fa)
        {
            pass[x] += dfs(g, p, pass,g[x][i], x);
        }
    }
    pass[x] += p[x];
    return pass[x];
}
int res = 0;
void dfs1(vector<vector<int>>& g, vector<int>& h, vector<int>& happy, int x, int fa)
{
    int sum = 0;
    for (int i = 0; i < g[x].size(); i++)
    {
        if (g[x][i] != fa)
        {
            sum += happy[g[x][i]];
            dfs1(g, h, happy, g[x][i], x);
        }
    }
    if (sum > happy[x])
        res++;
}

int main() {
    ios::sync_with_stdio(false);cin.tie(nullptr);

    int T;
    cin >> T;
    while (T--)
    {
        int n, m;
        cin >> n >> m;
        vector<vector<int>> g(n+1);
        vector<int> p(n + 1), h(n + 1), pass(n + 1),happy(n + 1);
        rep(i, 1, n)
        {
            cin >> p[i];
        }
        rep(i, 1, n)
        {
            cin >> h[i];
        }
        rep(i, 1, n - 1)
        {
            int u, v;
            cin >> u >> v;
            g[u].push_back(v);
            g[v].push_back(u);
        }
        res = 0;
        dfs(g, p, pass, 1, -1);
        rep(i, 1, n)
        {
            if ((h[i] + pass[i]) % 2)
                res++;
            happy[i] = (h[i] + pass[i]) / 2;
        }
        rep(i, 1, n)
        {
            if (happy[i] > pass[i] || happy[i] < 0)
                res++;
        }
        dfs1(g, h, happy, 1, -1);
        if (res)
            cout << "NO" << endl;
        else
            cout << "YES" << endl;
    }
}

 

另一种整洁一点的写法(一样的思路)

 

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
#include <cassert>
//#include <unordered_set>
//#include <unordered_map>
#define ll              long long
#define pii             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define forn(i, n)      for(int i = 0; i < int(n); i++)
using namespace std;
int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-6;
const int mod = 1e9 + 7;

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
inline ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
void exgcd(ll A, ll B, ll& x, ll& y)
{
    if (B) exgcd(B, A % B, y, x), y -= A / B * x; else x = 1, y = 0;
}
inline int qpow(int x, ll n) {
    int r = 1;
    while (n > 0) {
        if (n & 1) r = 1ll * r * x % mod;
        n >>= 1; x = 1ll * x * x % mod;
    }
    return r;
}
inline int inv(int x) {
    return qpow(x, mod - 2);
}
ll lcm(ll a, ll b)
{
    return a * b / gcd(a, b);
}
/**********************************************************/
const int N = 5e3 + 5;
int ans = 0;
int dfs(vector<vector<int>>& g, vector<int>& p, vector<int>& pass, vector<int>& happy, vector<int>& h ,int x,int fa)
{
    int sum = 0;
    for (int i = 0; i < g[x].size(); i++)
    {
        if (g[x][i] != fa)
        {
            pass[x] += dfs(g, p, pass, happy,h, g[x][i], x);
            sum += happy[g[x][i]];
        }
    }
    pass[x] += p[x];
    if ((pass[x] + h[x]) % 2)
        ans++;
    happy[x] = (pass[x] + h[x]) / 2;
    if (happy[x]<0 || happy[x]>pass[x])
        ans++;
    if (sum > happy[x])
        ans++;
    return pass[x];
}
int main() {
    ios::sync_with_stdio(false);cin.tie(nullptr);

    int T;
    cin >> T;
    while (T--)
    {
        ans = 0;
        int n, m;
        cin >> n >> m;
        vector<vector<int>> g(n+1);
        vector<int> p(n + 1), h(n + 1), pass(n + 1),happy(n + 1);
        rep(i, 1, n)
        {
            cin >> p[i];
        }
        rep(i, 1, n)
        {
            cin >> h[i];
        }
        rep(i, 1, n - 1)
        {
            int u, v;
            cin >> u >> v;
            g[u].push_back(v);
            g[v].push_back(u);
        }
        dfs(g, p, pass,happy,h,1,-1);
        if (ans)
            cout << "NO" << endl;
        else
            cout << "YES" << endl;
    }
}

 

posted @ 2020-07-31 22:22  DeaL57  阅读(282)  评论(0编辑  收藏  举报