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
#ifdef USE_PALI
#include <pali.hpp>
#else
#include <bits/stdc++.h>
#endif
using namespace std;
typedef unsigned int uint;

int main()
{
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  uint K, n1;
  cin >> K >> n1;
  if (K == 1) {
    cout << n1 << '\n';
    return 0;
  }

  vector<vector<uint>> M(K);
  M[0] = vector<uint>(n1, 0);
  vector<vector<uint>> V(K);
  V[0] = vector<uint>(n1, 0);
  for (uint i = 1; i < K; ++i) {
    uint ni;
    cin >> ni;
    M[i].reserve(ni);
    V[i].reserve(ni);
    for (uint j = 0; j < ni; ++j) {
      uint a;
      cin >> a;
      M[i].push_back(a);
      V[i].push_back(0);
    }
  }

  uint result = 0;
  for (int i_ = static_cast<int>(K - 1); i_ >= 0; --i_) {
    uint i = static_cast<uint>(i_);
    uint s = 0;
    for (uint j = 0; j < M[i].size(); ++j) {
      uint v = V[i][j];
      if (v == 0)
        v = 1;
      if (M[i][j] != 0)
        V[i - 1][M[i][j] - 1] += v;
      s += v;
    }
    if (result < s)
      result = s;
  }
  cout << result << '\n';
}