#P15613. [ICPC 2021 Jakarta R] White-Black Tree

[ICPC 2021 Jakarta R] White-Black Tree

题目描述

The White-Black Tree is a game played by two players on a rooted tree of NN nodes. The nodes are numbered from 11 to NN and node 11 is the root. Each node in the tree has a color CiC_{i} of either 00 (representing black) or 11 (representing white) that can be changed according to the rule of the game.

Two opposing players play alternately. In their turn, the player chooses a white node in the tree; let the chosen node be xx. First, the color of node xx (CxC_{x}) is changed from white to black. Then, in the same turn, the player is allowed to change the color of zero or more nodes that are a descendant of node xx from white to black or black to white. A node yy is a descendant of a node xx if and only if the parent of node yy is either node xx or a descendant of node xx. The player who cannot make any move loses and the opposing player wins the game.

Your task in this problem is to determine who will win the game assuming both players play optimally; it means that if there exists a move that guarantees their win, then they will surely play that move.

For example, consider the following game with a rooted tree of N=7N = 7 nodes and let the initial colors be C1..7={0,1,1,0,0,0,1}C_{1..7} = \{0, 1, 1, 0, 0, 0, 1\}.

:::align{center} :::

There are three white nodes (node 22, 33, and 77) in which the first player can choose for their first move. In this example, there is a strategy for the first player to win the game. One of the optimal plays for the first player is to choose node 33, change C3C_{3} into 00 (black), and then change the color of node 66 and node 77 (both are node 33's descendant), i.e. C6C_{6} becomes 11 (white) and C7C_{7} becomes 00 (black). When it's the second player's turn, there are only two white nodes to choose from (node 22 and 66) and both of them do not have any descendants. No matter what the second player chooses for their turn, the first player will simply choose the remaining one white node so that the second player will not be able to make any move. Therefore, the second player loses and the first player wins this example game.

输入格式

Input begins with a line containing an integer NN (2N1000002 \leq N \leq 100 000) representing the number of nodes in the given tree. The second line contains N1N - 1 integers PiP_{i} (1Pi<i1 \leq P_{i} < i) for i=2..Ni = 2..N representing the parent of node ii. The third line contains NN integers CiC_{i} (Ci{0,1}C_{i} \in \{0, 1\}) representing the initial color of node ii. The color black is represented by the integer 00 while the color white is represented by the integer 11.

输出格式

Output a string "First" in a line if the first player will win the game assuming both players play the game optimally. Otherwise, output a string "Second" in a line.

7
1 1 1 3 3 3
0 1 1 0 0 0 1
First
5
1 1 2 3
0 1 1 0 0

Second
4
1 1 1
1 1 0 1
First

提示

Explanation for the sample input/output #2

The black root has two white children with the same subtree structure and colors. Whatever move the first player makes on one child, the second player simply mimics them on the other child.

Explanation for the sample input/output #3

The first player can make all nodes to be black in one move by choosing node 11.