This page is still under construction.

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

Mixing Bowls (Large)

Time limit5sMemory limit512 MB

Summary
Given a recipe where each mixture's ingredients are other mixtures, find the minimum number of bowls needed to prepare it.
Level

Medium7 of 10

Topics
Tree, DFS, Greedy, Dynamic programming
Solved
No attempts yet

Problem

You are following a recipe to make your lunch.

The recipe is a mixture made by combining ingredients in a bowl. Each ingredient is one of two things:

  • another mixture, which you must make first in a separate bowl
  • a basic ingredient you already have in your kitchen, which you can add directly

To make a mixture, get all of its ingredients ready, take an empty bowl, and mix the ingredients in it. You cannot make a new mixture by adding ingredients to a bowl that already holds a mixture.

For example, to make the mixture CAKE out of the mixture CAKEMIX and the basic ingredient lies, you must first make CAKEMIX in its own bowl, then add CAKEMIX and lies to a second bowl to make CAKE.

Once you have used a mixture as an ingredient and emptied the bowl it was prepared in, you can reuse that bowl for another mixture. The number of bowls you need to prepare the recipe therefore depends on the order in which you make the mixtures.

Find the minimum number of bowls you need.

Input

The first line contains an integer CC, the number of test cases.

Each test case has this format:

  • one line with an integer NN, the number of mixtures in the test case
  • NN lines, one per mixture, each holding the mixture name, an integer MM giving the number of ingredients of that mixture, and the names of the MM ingredients

Tokens on one line are separated by single spaces.

The first mixture of a test case is the recipe you are making.

Mixture names are strings of 1 to 20 uppercase letters. Basic ingredient names are strings of 1 to 20 lowercase letters.

Every mixture except the recipe is used as an ingredient in exactly one other mixture, and the recipe is used in none. No name appears twice in the ingredient list of one mixture. No mixture requires itself, directly or indirectly.

Limits

  • 1≤C≤101 \le C \le 10
  • 1≤N≤10001 \le N \le 1000
  • 2≤M≤102 \le M \le 10

Output

For each test case, print one line in the form Case #X: Y, where XX is the test case number starting from 1 and YY is the minimum number of mixing bowls required.

Hint

In the first test case of the example input, you can make SOUP like this.

  1. Make VEGETABLES by mixing celery and onions in a bowl.
  2. Make STOCK in a second bowl by mixing chicken and the VEGETABLES from the first bowl. The first bowl becomes empty.
  3. Make SOUP in the now empty first bowl by mixing STOCK, salt and water.

In the second test case you choose whether to make FLAVOR or FRUIT first, before mixing them with milk and icecream to get MILKSHAKE.

Making FRUIT first uses four bowls.

  1. Make FRUIT in a bowl by mixing banana and berries.
  2. Make SPICES in a second bowl by mixing nutmeg and cinnamon, and CHOCOLATE in a third bowl by mixing cocoa and syrup, in either order.
  3. Make FLAVOR in a fourth bowl by mixing SPICES and CHOCOLATE.
  4. Make MILKSHAKE in the now empty second or third bowl by mixing FRUIT, FLAVOR, milk and icecream.

Making FRUIT after FLAVOR uses only three bowls.

  1. Make SPICES in a bowl by mixing nutmeg and cinnamon, and CHOCOLATE in a second bowl by mixing cocoa and syrup, in either order.
  2. Make FLAVOR in a third bowl by mixing SPICES and CHOCOLATE.
  3. Make FRUIT in the now empty first bowl by mixing banana and berries.
  4. Make MILKSHAKE in the now empty second bowl by mixing FRUIT, FLAVOR, milk and icecream.

Examples3

  1. Example 1

    Input
    2
    3
    SOUP 3 STOCK salt water
    STOCK 2 chicken VEGETABLES
    VEGETABLES 2 celery onions
    5
    MILKSHAKE 4 milk icecream FLAVOR FRUIT
    FRUIT 2 banana berries
    FLAVOR 2 SPICES CHOCOLATE
    SPICES 2 nutmeg cinnamon
    CHOCOLATE 2 cocoa syrup
    
    Expected output
    Case #1: 2
    Case #2: 3
    
  2. Example 2

    Input
    1
    1
    A 2 inga ingb
    
    Expected output
    Case #1: 1
    
  3. Example 3

    Input
    2
    6
    PA 2 PB inga
    PB 2 PC ingb
    PC 2 PD ingc
    PD 2 PE ingd
    PE 2 PF inge
    PF 2 ingf ingg
    3
    QA 2 QB QC
    QB 2 inga ingb
    QC 2 ingc ingd
    
    Expected output
    Case #1: 2
    Case #2: 3