[JZOJ 5500] [Tsinghua Training 2017 Simulated 12.10] Nutritional meal

Description

JM is a good friend of DY.To thank JM for his concern over the years, DY decided to invite him to a nutritious fruit meal.
DY has a tree with n nodes, node 1 is the root.There are many fruits growing at each node of the tree, among which there is ai fruit at node i and each fruit weighs bi.
The fruit is delicious, but the tree is very fragile! Once the total weight of the fruit on the child nodes of a node is too big, the branches can't stand the pressure and break! So you need to keep any node i:

ai<=∑c∈Child(i)ac∗bc

(The data guarantees that the initial situation meets this condition).
JM said,'Thank you for the fruits, but I don't have any internal fluctuations, and I even want to play games. We take turns: select a node, pick several fruits, you can't leave them. No operator can lose.
It doesn't make sense, says DY. We're all smart enough that once the game decides who's first, the results are sure.
However, you don't know the result of the game when you eat melon in the front row, so you have to write a program to know who will win.

Solution

Obviously, you can make a difference between how many times you go and how many times you add to your father, that is, you can move so much to your father.
Apparently b=0 had no effect on the father, disconnected,
All that's left is a ladder problem. Straight singular or odd layers.

Code

#include <cstdio>
#include <algorithm>
#include <map>
#define fo(i,a,b) for(int i=a;i<=b;++i)
#define fod(i,a,b) for(int i=a;i>=b;--i)
#define efo(i,q) for(int i=A[q];i;i=B[i][0])
#define min(q,w) ((q)>(w)?(w):(q))
#define max(q,w) ((q)<(w)?(w):(q))
using namespace std;
const int N=50500;
int read(int &n)
{
    char ch=' ';int q=0,w=1;
    for(;(ch!='-')&&((ch<'0')||(ch>'9'));ch=getchar());
    if(ch=='-')w=-1,ch=getchar();
    for(;ch>='0' && ch<='9';ch=getchar())q=q*10+ch-48;n=q*w;return n;
}
int m,n,ans;
int a[N],b[N];
int B[2*N][2],A[N],B0;
void link(int q,int w)
{
    B[++B0][0]=A[q];A[q]=B0,B[B0][1]=w;
    B[++B0][0]=A[w];A[w]=B0,B[B0][1]=q;
}
void dfs(int q,int c,int fa)
{
    efo(i,q)if(B[i][1]!=fa)a[q]-=a[B[i][1]]*b[B[i][1]];
    if(!b[q])c=1;
    if(c&1)ans=ans^a[q];
    efo(i,q)if(B[i][1]!=fa)dfs(B[i][1],c+1,q);
}
int main()
{
    freopen("meal.in","r",stdin);
    freopen("meal.out","w",stdout);
    int q,w,_;
    for(read(_);_;--_)
    {
        read(n);
        fo(i,1,n)read(a[i]);
        fo(i,1,n)read(b[i]),A[i]=0;B0=0;
        fo(i,1,n-1)read(q),read(w),link(q,w);
        ans=0;
        dfs(1,1,0);
        if(ans)printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

Added by chinni_77 on Mon, 18 May 2020 19:57:10 +0300