QOJ.ac

QOJ

実行時間制限: 2 s メモリ制限: 256 MB 満点: 100

#4896. Alice, Bob and DFS

統計

Alice and Bob are planning to play another game.

There is a Directed Acyclic Graph (DAG) with $n$ vertices (labeled starting from $1$), where each vertex is either black or white. The graph may contain multiple edges, and the outgoing edges for each vertex are ordered.

There is a DFS program with the following pseudocode:

define procedure dfs(u):
    if u is a white vertex
        traverse all outgoing edges (u,v) of u in order
            read a boolean value g
            if g
                dfs(v)
    else
        traverse all outgoing edges (u,v) of u in order
            dfs(v)

read an integer r
dfs(r)

This program has a special property: it automatically pauses before the start of every dfs procedure call.

Alice and Bob start $k$ such programs, and for each program, they input a vertex label as $r$ (the input $r$ may differ between programs). Now, all these programs are paused after reading $r$ and before calling dfs(r).

Then the game begins. Alice goes first, and the players take turns performing the following operation:

  • Choose a program that has not yet finished execution and resume it from its paused state. Continue executing the program until it pauses again or finishes. During this process, every time the program reads $g$, the current player can choose whether the result is true or false.

Each program will finish after a finite number of pauses, and eventually, all programs will finish execution. If it is a player's turn and all programs have finished execution, that player loses the game, and the other player wins.

Both players play optimally. Who will win?

Input

The first line contains a positive integer $n$, the number of vertices in the graph.

The second line contains $n$ integers $c_1, c_2, \dots, c_n$, representing the color of each vertex. $c_u=0$ means $u$ is a black vertex, and $c_u=1$ means $u$ is a white vertex.

The next $n$ lines describe the edges in the graph. The $i$-th line contains a non-negative integer $m_i$ followed by $m_i$ positive integers $e_{i,1}, e_{i,2}, \dots, e_{i,m_i}$, representing the number of outgoing edges from $i$ and the destination of each edge, respectively. For convenience, it is guaranteed that for all $1 \le i \le n$ and $1 \le j \le m_i$, $e_{i,j} > i$; that is, $1, 2, \dots, n$ is a topological order of the graph.

The next line contains a positive integer $k$, the number of programs.

The last line contains $k$ positive integers $r_1, r_2, \dots, r_k$, representing the input for each program before the game starts.

Output

A single line containing a string representing the winner. If Alice wins, output Alice; if Bob wins, output Bob.

Examples

Input 1

4
1 0 1 0
1 2
2 4 3
1 4
0
1
2

Output 1

Alice

Note 1

The graph has $4$ vertices.

  • Vertex $1$: White, one outgoing edge to $2$.
  • Vertex $2$: Black, two outgoing edges to $4$ and $3$.
  • Vertex $3$: White, one outgoing edge to $4$.
  • Vertex $4$: Black, no outgoing edges.

There is only one program, with initial vertex $r=2$. The execution process when both players play optimally is as follows:

  1. Input initial vertex $r=2$ and pause. The game starts, and it is Alice's turn. Since there is only one program, both players must choose this program. Alice chooses to resume the program.
  2. dfs(2)
    1. Pause. It is Bob's turn. Bob chooses to resume the program.
    2. dfs(4)
      1. Procedure returns.
    3. Pause. It is Alice's turn. Alice chooses to resume the program.
    4. dfs(3)
      1. Read a boolean value $g$. Alice chooses false.
      2. Procedure returns.
    5. Procedure returns.
  3. Execution finishes. It is Bob's turn.

When it is Bob's turn, all programs have finished, so Bob loses and Alice wins.

In this example, the existence of vertex $1$ does not affect the game.

Additionally, if Alice chose true in dfs(3), then dfs(3) would call dfs(4), pausing one extra time before the call. This would result in it being Alice's turn when the program finishes, and the winner would be Bob. Since both players play optimally, this does not happen.

Input 2

5
1 1 0 0 1
2 3 5
2 3 5
2 5 4
0
0
2
1 2

Output 2

Bob

Note 2

The two programs have initial vertices $1$ and $2$. Vertices $1$ and $2$ have the same color and outgoing edges, differing only in their labels.

Bob can adopt the following strategy: whenever Alice chooses to execute a program, Bob chooses to execute the other program and provides the same input. It is easy to prove that this is a winning strategy for Bob.

Input 3

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

Output 3

Bob

Constraints

For all data:

  • $1 \le n \le 2 \times 10^5$
  • $c_i \in \{0, 1\}$ (for all $1 \le i \le n$)
  • $0 \le m_i \le 4 \times 10^5$ (for all $1 \le i \le n$)
  • $\sum_{i=1}^n m_i \le 4 \times 10^5$
  • $i < e_{i,j} \le n$ (for all $1 \le i \le n, 1 \le j \le m_i$)
  • $1 \le k \le 2 \times 10^5$
  • $1 \le r_i \le n$ (for all $1 \le i \le k$)
Subtask Score Special Constraints
$1$ $5$ $c_i=0$ (for all $1 \le i \le n$)
$2$ $15$ $k=1, r_1=1$
$3$ $15$ $n \le 10, \sum_{i=1}^n m_i \le 10, k \le 10$
$4$ $20$ $c_i=1$ (for all $1 \le i \le n$); each vertex $u$ has at most one incoming edge; for all $1 \le i \le k$, no edge in the graph ends at $r_i$
$5$ $20$ $c_i=1$ (for all $1 \le i \le n$)
$6$ $25$ No additional constraints

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download
#1812EditorialOpen线性做法Anonymous2026-05-26 20:26:43View

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.