1
+ import java .io .PrintStream ;
2
+ import java .util .*;
3
+ import java .util .stream .Collectors ;
4
+
5
+ /**
6
+ * Codejam 2012 Round 1C
7
+ * Problem A. Diamond Inheritance
8
+ */
9
+ public class Main {
10
+
11
+ private String solve (Scanner scanner ) {
12
+ int n =scanner .nextInt ();
13
+ ArrayList <Integer >[] lists =new ArrayList [n ];
14
+ for (int i =0 ;i <n ;i ++) lists [i ]=new ArrayList <>();
15
+ boolean [][] able =new boolean [n ][n ];
16
+ for (int i =0 ;i <n ;i ++) {
17
+ able [i ][i ]=true ;
18
+ int k =scanner .nextInt ();
19
+ while (k -->0 ) {
20
+ int x =scanner .nextInt ()-1 ;
21
+ lists [i ].add (x );
22
+ able [i ][x ]=true ;
23
+ }
24
+ }
25
+ for (int k =0 ;k <n ;k ++) {
26
+ for (int i =0 ;i <n ;i ++) {
27
+ for (int j =0 ;j <n ;j ++) {
28
+ if (able [i ][k ] && able [k ][j ])
29
+ able [i ][j ]=true ;
30
+ }
31
+ }
32
+ }
33
+ for (int i =0 ;i <n ;i ++) {
34
+ ArrayList <Integer > list =lists [i ];
35
+ for (int u =0 ;u <list .size ();u ++) {
36
+ for (int v =u +1 ;v <list .size ();v ++) {
37
+ for (int w =0 ;w <n ;w ++) {
38
+ if (able [list .get (u )][w ] && able [list .get (v )][w ])
39
+ return "Yes" ;
40
+ }
41
+ }
42
+ }
43
+ }
44
+ return "No" ;
45
+ }
46
+
47
+ public static void main (String [] args ) throws Exception {
48
+ System .setOut (new PrintStream ("output.txt" ));
49
+ Scanner scanner =new Scanner (System .in );
50
+ int times =scanner .nextInt ();
51
+ long start =System .currentTimeMillis ();
52
+ for (int t =1 ;t <=times ;t ++) {
53
+ try {
54
+ System .out .println (String .format ("Case #%d: %s" , t , new Main ().solve (scanner )));
55
+ }
56
+ catch (Throwable e ) {
57
+ System .err .println ("ERROR in case #" +t );
58
+ e .printStackTrace ();
59
+ }
60
+ }
61
+ long end =System .currentTimeMillis ();
62
+ System .err .println (String .format ("Time used: %.3fs" , (end -start )/1000.0 ));
63
+
64
+ }
65
+
66
+ }
0 commit comments