Under construction

Still being built. Things may change

Optimal alpha beta pruning

Time limit2sMemory limit512 MB

Summary
Given a game tree with leaf values, find the minimum and maximum number of leaves alpha-beta pruning evaluates over all child orderings.
Level

Medium7 of 10

Topics
Tree, Minimum spanning tree, Dynamic programming
Solved
No attempts yet

Problem

Fox Ciel is developing an artificial intelligence (AI) for a game. The game is described as a game tree TT with nn vertices. Each node has an evaluation value that shows how good a situation is. The value of a non-leaf node is the maximum of its children's values multiplied by −1-1. Leaf values are computed with Ciel's special function, which is heavy to evaluate. So Ciel uses alpha-beta pruning to compute the root's evaluation value while reducing the number of leaf nodes that are evaluated.

The order in which child nodes are evaluated changes how many leaf nodes are computed. Ciel wants the minimum and the maximum number of leaf evaluations she can get when she may evaluate the children of each node in any order. Ciel uses the following algorithm.

function negamax(node, α, β)
    if node is a terminal node
        return value of leaf node
    else
        foreach child of node
            val := -negamax(child, -β, -α)
            if val >= β
                return val
            if val > α
                α := val
        return α

negamax algorithm

Input

The first line contains an integer nn, the number of vertices in the game tree TT. The second line contains nn integers pip_i, the evaluation value of vertex ii. The next nn lines describe the tree. kik_i is the number of children of vertex ii, followed by the child indices tijt_{ij}.

n
p1 p2 ... pn
k1 t11 t12 ... t1k
:
:
kn tn1 tn2 ... tnk
  • 2≤n≤1002 \le n \le 100
  • −10000≤pi≤10000-10000 \le p_i \le 10000
  • 0≤ki≤50 \le k_i \le 5
  • 2≤tij≤n2 \le t_{ij} \le n
  • The root is vertex 11.
  • The evaluation value of a non-leaf node is always 00 in the input. You must compute it when needed.
  • A leaf node may also have value 00.
  • The game tree is a tree structure.

Output

Print the minimum and the maximum number of leaf evaluations, separated by a space.

minimum maximum

Examples4

  1. Example 1

    Input
    3
    0 1 1
    2 2 3
    0
    0
    
    Expected output
    2 2
    
  2. Example 2

    Input
    8
    0 0 100 100 0 -100 -100 -100
    2 2 5
    2 3 4
    0
    0
    3 6 7 8
    0
    0
    0
    
    Expected output
    3 5
    
  3. Example 3

    Input
    8
    0 0 100 100 0 100 100 100
    2 2 5
    2 3 4
    0
    0
    3 6 7 8
    0
    0
    0
    
    Expected output
    3 4
    
  4. Example 4

    Input
    19
    0 100 0 100 0 0 0 0 0 1 2 3 4 5 6 7 8 9 10
    2 2 3
    0
    2 4 5
    0
    3 6 7 8
    3 9 10 11
    3 12 13 14
    3 15 16 17
    2 18 19
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    
    Expected output
    7 12