This page is still under construction.

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

Mixing Bowls (Small)

Time limit5sMemory limit512 MB

Summary
Given a recipe tree of mixtures, find the minimum number of bowls needed by choosing the order of preparation.
Level

Medium6 of 10

Topics
Tree, DFS, Greedy
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, you need all of its ingredients ready. You then take an empty bowl and mix the ingredients in it. You cannot make a mixture by adding ingredients to a bowl that already holds a mixture.

For example, if you want 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 re-use 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.

Determine the minimum number of bowls you need.

Input

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

Each test case is given as follows.

  • One line containing an integer NN, the number of mixtures in the test case.
  • NN lines, one for each mixture, containing the mixture name, an integer MM giving the number of ingredients in this mixture, and then the names of the MM ingredients.

The tokens on one line are separated by single spaces.

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

The name of a mixture is a string of between 1 and 20 uppercase letters. The name of a basic ingredient is a string of between 1 and 20 lowercase letters.

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

Limits

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

Output

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

Hint

In the first test case of the example, you make SOUP with these steps.

  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 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.

If you make FRUIT first, you use 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 emptied second or third bowl by mixing FRUIT, FLAVOR, milk and icecream.

If you make FRUIT after FLAVOR, three bowls are enough.

  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 empty first bowl by mixing banana and berries.
  4. Make MILKSHAKE in the empty second bowl by mixing FRUIT, FLAVOR, milk and icecream.

Examples1

  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