#P13321. [GCJ 2012 #1C] Diamond Inheritance

    ID: 15186 远端评测题 4000ms 1024MiB 尝试: 0 已通过: 0 难度: 3 上传者: 标签>搜索2012深度优先搜索 DFSGoogle Code Jam

[GCJ 2012 #1C] Diamond Inheritance

题目描述

You are asked to help diagnose class diagrams to identify instances of diamond inheritance. The following example class diagram illustrates the property of diamond inheritance. There are four classes: A,B,CA, B, C and DD. An arrow pointing from XX to YY indicates that class XX inherits from class YY.

In this class diagram, DD inherits from both BB and CC, BB inherits from AA, and CC also inherits from AA. An inheritance path from XX to YY is defined as a sequence of classes XX, C1C_1, C2C_2, C3C_3, \dots, CnC_n, YY where XX inherits from C1C_1, CiC_i inherits from Ci+1C_{i+1} for 1in11 \leq i \leq n - 1, and CnC_n inherits from YY. There are two inheritance paths from DD to AA in the example above. The first path is DD, BB, AA and the second path is DD, CC, AA.

A class diagram is said to contain a diamond inheritance if there exists a pair of classes XX and YY such that there are at least two different inheritance paths from XX to YY. The above class diagram is a classic example of diamond inheritance. Your task is to determine whether or not a given class diagram contains a diamond inheritance.

输入格式

The first line of the input gives the number of test cases, TT. TT test cases follow, each specifies a class diagram. The first line of each test case gives the number of classes in this diagram, NN. The classes are numbered from 11 to NN. NN lines follow. The ithi^{th} line starts with a non-negative integer MiM_i indicating the number of classes that class ii inherits from. This is followed by MiM_i distinct positive integers each from 11 to NN representing those classes. You may assume that:

  • If there is an inheritance path from XX to YY then there is no inheritance path from YY to XX.
  • A class will never inherit from itself.

输出格式

For each diagram, output one line containing "Case #xx: yy", where xx is the case number (starting from 11) and yy is "Yes" if the class diagram contains a diamond inheritance, "No" otherwise.

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
Case #1: No
Case #2: Yes
Case #3: Yes

提示

Limits

  • 1T501 \leq T \leq 50.
  • 0Mi100 \leq M_i \leq 10.

Test set 1 (14 Pts, Visible Verdict)

  • 1N501 \leq N \leq 50.

Test set 2 (14 Pts, Hidden Verdict)

  • 1N1,0001 \leq N \leq 1,000.