1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <cstdio>
#include <vector>

using namespace std;

#define DBG(X)

vector<int> tab[101];

int dp[101];

int token[101];


int main()
{
  int n;
  scanf("%d", &n);
  
  for (int i = 0; i < n; i++)
  {
    int r;
    scanf("%d", &r);
    for (int j = 0; j < r; j++)
    {
      int l;
      scanf("%d", &l);
      l--;
      tab[i].push_back(l);
    }
  }
  
  int loops = 0;
  
  dp[0] = 1;
  
  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < tab[i].size(); j++)
    {
      dp[tab[i][j]] += dp[i];
    }
  }
  
  for (int i = 0; i < n; i++)
  {
    DBG(printf("%d %d\n", i, dp[i]);)
  }
  
  
  
  int sum = 0;
  for (int i = 1; i < 1000000000; i++)
  {
    if (tab[0].size() == 0) 
    {
      loops = 1;
      break;
    }
    
    int w = tab[0][token[0]];
    token[0]++;
    sum++;
    if (token[0] == tab[0].size()) 
    {
      sum -= token[0];
      token[0] = 0;
    }
    

    while (w != 0)
    {
      //printf("w %d\n", w);
      
      if (tab[w].size() == 0) break;
      int a = tab[w][token[w]];
      //printf("w %d %d\n", w, token[w]);
      token[w]++;
      sum++;
      if (token[w] >= tab[w].size()) 
      {
        sum -= token[w];
        token[w] = 0;
      }
      w = a;
    }
    
    //sum = 0;
    //for (int i = 0; i < n; i++)  sum += token[i];
    
    loops++;
    if (sum == 0) break;
  }
  printf("%d\n", loops);
  
  
  return 0;
}