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
99
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define JOIN_(X, Y) X##Y
#define JOIN(X, Y) JOIN_(X, Y)
#define TMP JOIN(tmp, __LINE__)
#define PB push_back
#define SZ(x) int((x).size())
#define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i)
#define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i)
#define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i)

#ifdef DEBUG
#define DEB(x) (cerr << x)
#else
#define DEB(x)
#endif

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef unsigned int uint;

const int INF = 1e9 + 9;

struct Case {
  vi t;
  int k;
  vector<vi> prev;
  vector<vi> cnt;

  void solve() {
    cin >> k;
    prev.resize(k);
    cnt.resize(k);
    REP(i, k) {
      int n;
      cin >> n;
      t.PB(n);
      if (i > 0) {
        REP(j, n) {
          int a;
          cin >> a;
          prev[i].PB(a);
          cnt[i].PB(0);
        }
      } else {
        REP(j, n) {
          prev[i].PB(0);
          cnt[i].PB(0);
        }
      }
    }

    int result = 0;
    FORD(i, k - 1, 0) {
      int n = t[i];
      DEB("i = " << i << " n = " << n << "\n");
      int current = 0;
      REP(j, SZ(cnt[i])) {
        int c = max(cnt[i][j], 1);
        DEB("  j = " << j << " c = " << c << "\n");
        current += c;
        DEB("  prev[i][j] = " << prev[i][j] << "\n");
        if (prev[i][j] > 0) {
          DEB("  cnt before " << cnt[i - 1][prev[i][j] - 1] << "\n");
          cnt[i - 1][prev[i][j] - 1] += c;
          DEB("  cnt after " << cnt[i - 1][prev[i][j] - 1] << "\n");
        }
      }
      result = max(result, current);
    }
    cout << result << "\n";
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int z = 1;
  // cin >> z;
  while (z--) {
    Case().solve();
  }
}