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
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

struct Meeting {
  long day;
  long participants = 0;
  vector<long> continuations;

  Meeting() : day(0), participants(0) {}
  Meeting(long day) : day(day) {}
};

void aggregate(long k, unordered_map<long, Meeting>& meetings, vector<long>& participants_per_day) {
  const Meeting& m = meetings[k];
  if(m.continuations.empty()) {
    meetings[k].participants = 1;
    participants_per_day[m.day]++;
    return;
  }
  for(const long continuation_id : m.continuations) {
    meetings[k].participants+=meetings[continuation_id].participants;
  }
  participants_per_day[m.day]+=m.participants;
}

int main() {
  ios_base::sync_with_stdio(false);
  unordered_map<long, Meeting> meetings;
  

  long k, n;
  cin >> k >> n;

  vector<long> participants_per_day(k, 0);
  vector<vector<long>> meetings_per_day(k);
  for(int i = 0; i < n; i++) {
    meetings[i] = Meeting(0);
    meetings_per_day[0].push_back(i);
  }
  for(long day = 1; day < k; day++) {
    long a, b;
    cin >> a;
    for(long i =0; i < a ; i++) {
      long id = day * 500000 + i; 
      cin >> b;
      meetings[id] = Meeting(day);
      if(b > 0) {
        long parent = (day-1) * 500000 + b - 1;
        meetings[parent].continuations.push_back(id);
      }
      meetings_per_day[day].push_back(id);
    }
  }

  long result = n;
  for(long day = k-1; day >= 0; day--) {
    for(long id : meetings_per_day[day]) {
      aggregate(id, meetings, participants_per_day);
    }
    result = max(result, participants_per_day[day]);
  }
  cout << result << endl;
}