All problems

Problem 922

Binary Tree Coloring Game

Medium · 20 pts · Tree, Depth-First Search, Game Theory

Problem

A binary tree has N nodes (N odd) with distinct values 1 to N. The first player colours the node X red, then the second player colours another node blue. Then they alternate: each colours an uncoloured neighbour (parent or child) of one of their own nodes, or passes if they cannot. When nobody can move, whoever coloured more nodes wins. Decide whether the second player can choose a node that guarantees a win: X splits the tree into its left subtree, right subtree and the rest, and blue wins by taking more than half of one of them.

Input

The first line contains X. The next two lines describe the tree: N, the number of tokens, then N level-order tokens with 'null' for missing children. The values are 1 to N.

Output

Print YES if the second player can win, otherwise NO.

Constraints

1 <= N <= 100; N is odd; the values are exactly 1 to N; 1 <= X <= N

Example input

3
11
1 2 3 4 5 6 7 8 9 10 11

Example output

YES

Explanation

In the tree 1 2 3 4 5 6 7 8 9 10 11 with X = 3, the part of the tree outside node 3's subtree has 8 of the 11 nodes. Blue colours node 1 there and takes all 8, so the answer is YES.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Closest Binary Search Tree Value Next: Binary Tree Longest Consecutive Sequence