This page is still under construction.

Parts of this page are still being built. What you see may change.

Web Service Dependencies

Time limit1sMemory limit256 MB

Summary
Count the launch orders that place each container after all of its dependencies for each configuration.
Level

Medium6 of 10

Topics
Dynamic programming, Topological sort, Bit manipulation
Solved
No attempts yet

Problem

Docker is an open platform for building, shipping and running distributed applications as lightweight containers. Think of a container as a lightweight virtual machine that runs in user mode. Amazon's EC2 Container Service (ECS) uses Docker containers to build a higher-level abstraction called a task. A task is one or more containers together with their dependencies, and ECS launches those containers in a correct order. A container can be launched only when every container it depends on is already running. Amazon wants to compare performance across different valid launch orders, so ECS needs to know how many valid launch orders exist. Count them for each configuration.

Input

The first line contains the number of configurations NN (1≤N≤1001 \le N \le 100).

The first line of each configuration contains an integer MM (1≤M≤121 \le M \le 12), the number of containers. Each of the next MM lines contains a space-separated list of alphanumeric names. The first name on a line is a container, and the remaining names on that line are the containers it depends on, given in arbitrary order.

Every name is at most 10 characters long, and names are case-sensitive. Each of the MM containers appears as the first name of exactly one line, and every name written as a dependency is one of those MM containers. The same dependency may appear more than once on a line, and a line may name its own container as a dependency.

Output

For each configuration print Case #n: , where nn is the configuration number starting at 1, followed by the number of valid launch orders. Print 0 when no valid order exists.

Examples3

  1. Example 1

    Input
    3
    3
    a b c
    b c
    c
    6
    c1 c2
    c2 c3
    c4 c5
    c3 c5
    c5 c6
    c6
    1
    a a
    
    Expected output
    Case #1: 1
    Case #2: 4
    Case #3: 0
    
  2. Example 2

    Input
    1
    1
    a
    
    Expected output
    Case #1: 1
    
  3. Example 3

    Input
    1
    12
    c1
    c2
    c3
    c4
    c5
    c6
    c7
    c8
    c9
    c10
    c11
    c12
    
    Expected output
    Case #1: 479001600