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

using namespace std;


#define int long long

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
        cout.tie(0);

	int k; int N;
        cin>>k>>N;
	vector<int>A(k+1);
	A[1] = N;

	vector<vector<int>>prew(k+1);
	prew[1].assign(A[1]+1, 0);

	for(int i=2; k>=i; i++){
		int x; cin>>x;
		A[i] = x;
		prew[i].assign(x+1,0);
		for(int j=1; x>=j; j++){
			cin>>prew[i][j];
		}
	}

	vector<vector<int>> need(k+1);
	for(int i=1; k>=i; i++){
		need[i].assign(A[i]+1, 0);
	}

	int ans = 0;

	if(k >= 1){
		int sumDay=0;
		for(int j=1; A[k]>=j; j++){
			need[k][j] = 1;
			sumDay += 1;
		}
		ans = sumDay;
	}

	for(int day=k-1; day>=1; day--){
		vector<int> sumChild(A[day]+1,0);
		for(int j=1; A[day+1]>=j; j++){
			int p = prew[day+1][j];
			if(p>0){
				sumChild[p] += need[day+1][j];
			}
		}
		int sumDay=0;
		for(int x=1; A[day]>=x; x++){
			int v=sumChild[x];
			if(v<1) v=1;
			need[day][x]=v;
			sumDay+=v;
		}
		if(sumDay>ans) ans = sumDay;
	}

	cout<< ans;;
}