Under construction

Still being built. Things may change

Diamond Inheritance (Large)

Interview

Time limit5sMemory limit512 MB

Summary
Decide whether any pair of classes in each inheritance DAG has two different inheritance paths between them.
Level

Medium5 of 10

Topics
Graph, Topological sort, Dynamic programming
Solved
No attempts yet

Problem

You inspect class diagrams and decide whether they contain a diamond inheritance. The diagram below has four classes AA, BB, CC and DD. An arrow from XX to YY means that class XX inherits from class YY.

In this diagram DD inherits from both BB and CC, BB inherits from AA, and CC also inherits from AA.

An inheritance path from class XX to class YY is a sequence of classes X,C1,C2,…,Cn,YX, C_1, C_2, \dots, C_n, Y in which XX inherits from C1C_1, class CiC_i inherits from Ci+1C_{i+1} for every ii with 1≤i≤n−11 \le i \le n - 1, and CnC_n inherits from YY. The case n=0n = 0 counts too: if XX inherits from YY directly, the sequence X,YX, Y is itself one inheritance path. The example above has two inheritance paths from DD to AA, namely D,B,AD, B, A and D,C,AD, C, A.

A class diagram contains a diamond inheritance if some pair of classes XX, YY has at least two different inheritance paths from XX to YY. The diagram above is the standard example. Decide whether a given class diagram contains a diamond inheritance.

Input

The first line holds the number of test cases TT. Then TT test cases follow, each describing one class diagram.

The first line of a test case holds the number of classes NN in that diagram. The classes are numbered from 11 to NN. Of the NN lines that follow, line ii starts with the number of classes that class ii inherits from, MiM_i, followed by MiM_i distinct integers between 11 and NN. Those integers are the numbers of the classes that class ii inherits from.

The input satisfies the following.

  • If there is an inheritance path from XX to YY, then there is no inheritance path from YY to XX.
  • No class inherits from itself.

Limits

  • 1≤T≤501 \le T \le 50
  • 1≤N≤10001 \le N \le 1000
  • 0≤Mi≤100 \le M_i \le 10

Output

For each diagram print one line Case #x: y, where xx is the test case number starting from 11, and yy is Yes if that diagram contains a diamond inheritance and No if it does not.

Examples2

  1. Example 1

    Input
    3
    3
    1 2
    1 3
    0
    5
    2 2 3
    1 4
    1 5
    1 5
    0
    3
    2 2 3
    1 3
    0
    
    Expected output
    Case #1: No
    Case #2: Yes
    Case #3: Yes
    
  2. Example 2

    Input
    1
    1
    0
    
    Expected output
    Case #1: No