#C9277. Binary Tree Construction Possibility
Binary Tree Construction Possibility
Binary Tree Construction Possibility
You are given a set of nodes labeled from 0 to n-1. For each node, you are given a parent index and an assigned value. The parent array uses -1 to denote the root node, and each node other than the root has a valid parent index. The tree must be a binary tree, meaning that every node has at most two children and there is exactly one root.
Define a DFS function such that for a node, the computed result is obtained by taking the bitwise XOR of the node's own value with the results of DFS on its children. The condition for a valid binary tree is that the DFS result computed from the root is equal to the value assigned to the root.
Your task is to determine whether it is possible to construct such a binary tree with the given parent-child relationships and node values that satisfies the condition \( DFS(root) = value[root] \).
inputFormat
The input is read from stdin and contains three lines:
- The first line contains an integer ( n ) (1 ≤ n ≤ 10^5), the number of nodes.
- The second line contains ( n ) space-separated integers representing the parent of each node. For the root node, the parent is -1; for all other nodes, the parent is between 0 and n-1.
- The third line contains ( n ) space-separated integers where the ( i^{th} ) integer is the value assigned to node ( i ) (0 ≤ value[i] < 2^{31}).
outputFormat
Print a single line to stdout: "YES" if it is possible to construct a binary tree that satisfies the conditions, or "NO" otherwise.## sample
5
-1 0 0 1 1
1 2 3 2 3
YES