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 𝑛 cities and 𝑛−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 𝑛 and the city 1 is a capital. In other words, the country has a tree structure.

There are 𝑚 citizens living in the country. A 𝑝𝑖 people live in the 𝑖-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 𝑖-th city calculates a happiness index ℎ𝑖 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 𝑖 the happiness index will be equal exactly to ℎ𝑖”.

Input
The first line contains a single integer 𝑡 (1≤𝑡≤10000) — the number of test cases.

The first line of each test case contains two integers 𝑛 and 𝑚 (1≤𝑛≤105; 0≤𝑚≤109) — the number of cities and citizens.

The second line of each test case contains 𝑛 integers 𝑝1,𝑝2,…,𝑝𝑛 (0≤𝑝𝑖≤𝑚; 𝑝1+𝑝2+…+𝑝𝑛=𝑚), where 𝑝𝑖 is the number of people living in the 𝑖-th city.

The third line contains 𝑛 integers ℎ1,ℎ2,…,ℎ𝑛 (−109≤ℎ𝑖≤109), where ℎ𝑖 is the calculated happiness index of the 𝑖-th city.

Next 𝑛−1 lines contain description of the roads, one per line. Each line contains two integers 𝑥𝑖 and 𝑦𝑖 (1≤𝑥𝑖,𝑦𝑖≤𝑛; 𝑥𝑖≠𝑦𝑖), where 𝑥𝑖 and 𝑦𝑖 are cities connected by the 𝑖-th road.

It’s guaranteed that the sum of 𝑛 from all test cases doesn’t exceed 2⋅105.

Output
For each test case, print YES, if the collected data is correct, or NO — otherwise. You can print characters in YES or NO in any case.

Examples
inputCopy
2
7 4
1 0 1 1 0 1 0
4 0 0 -1 0 -1 0
1 2
1 3
1 4
3 5
3 6
3 7
5 11
1 2 5 2 1
-11 -2 -6 -2 -1
1 2
1 3
1 4
3 5
outputCopy
YES
YES
inputCopy
2
4 4
1 1 1 1
4 1 -3 -1
1 2
1 3
1 4
3 13
3 3 7
13 1 4
1 2
1 3
outputCopy
NO
NO
Note
Let’s look at the first test case of the first sample:

感觉这场题目都挺毒瘤的,C题题意特别毒瘤。

题意:
每个城市最后会有一些人。一开始所有人都在1城市,每一秒钟每个人向下(深度更深)走一格或者不走。每个人有初始心情,好或者坏。走一段路可以由好心情变成坏心情,不能由坏心情变成好心情。一个城市的值为经过该城市的好心情人数减掉坏心情人数,问能否使得第i个城市的值为h[i]h[i]h[i]

思路:
每个点算值的时候,人数就等于该点子树人数和。
假设经过点xxx好心情人数为f1[x]f1[x]f1[x],坏心情人数为f2[x]f2[x]f2[x]
则有
f1[x]+f2[x]=size[x]f1[x]+f2[x]=size[x]f1[x]+f2[x]=size[x]
f1[x]−f2[x]=h[x]f1[x]-f2[x]=h[x]f1[x]f2[x]=h[x]

同时必须满足f1[x]+f2[x]≥∑(f1[v]+f2[v])f1[x]+f2[x]≥∑(f1[v]+f2[v])f1[x]+f2[x](f1[v]+f2[v])v为x子节点v为x子节点vx,因为节点x的人可以不全部往下走。
f1[x]≥∑f1[v]f1[x]≥∑f1[v]f1[x]f1[v],保证好心情人数不会增多。

就是搞完这几个限定条件就好了,一道分类讨论的构造题。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <iostream>
#include <map>
#include <string>
#include <set>

typedef long long ll;
using namespace std;

const int maxn = 1e5 + 7;

vector<int>G[maxn];
ll p[maxn],b[maxn];
ll siz[maxn];
ll f1[maxn],f2[maxn];

bool dfs(int x,int fa) {
    siz[x] = p[x];
    for(int i = 0;i < G[x].size();i++) {
        int v = G[x][i];
        if(v == fa) continue;
        if(!dfs(v,x)) return false;
        siz[x] += siz[v];
    }
    
    f1[x] = (siz[x] + b[x]) / 2;
    f2[x] = (siz[x] - b[x]) / 2;
    ll num1 = 0,num2 = 0;
    for(int i = 0;i < G[x].size();i++) {
        int v = G[x][i];
        if(v == fa) continue;
        num1 += f1[v];
        num2 += f2[v];
    }
    if(f1[x] < 0 || f2[x] < 0) return false;
    if(f1[x] + f2[x] != siz[x]) return false;
    if(num1 + num2 > f1[x] + f2[x]) return false;
    if(num1 > f1[x]) return false;
    if(f1[x] - f2[x] != b[x]) return false;
    return true;
}

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("%lld",&p[i]);
            G[i].clear();
        }
        for(int i = 1;i <= n;i++) {
            scanf("%lld",&b[i]);
        }
        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);
        }
        if(dfs(1,-1)) printf("YES\n");
        else printf("NO\n");

    }
    return 0;
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐