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
// PA2026, @mjm, r1b-kon

#include <cstdio>
#include <vector>
using namespace std;
int nextInt() { int n; scanf("%d", &n); return n; }

struct Node {
	int prev;
	int cnt;
};

int myMax(int a, int b) { return a >= b ? a : b; }

int solve() {
	int k = nextInt();
	vector<vector<Node>> graph(k);
	int n = nextInt();
	graph[0].resize(n, { -1, 0 });
	for (int d = 1; d < k; ++d) {
		n = nextInt();
		graph[d].resize(n, { -1, 0 });
		for (int i = 0; i < n; ++i)
			graph[d][i].prev = nextInt() - 1;
	}
	int res = 0;
	for (int d = k - 1; d >= 0; --d) {
		int cur = 0;
		for (int i = 0; i < graph[d].size(); ++i) {
			if (0 == graph[d][i].cnt)
				graph[d][i].cnt = 1;
			cur += graph[d][i].cnt;
			int pr = graph[d][i].prev;
			if (pr >= 0)
				graph[d - 1][pr].cnt += graph[d][i].cnt;
		}
		res = myMax(res, cur);
		//fprintf(stderr, "day %d cur %d\n", d, cur);
	}
	return res;
}

int main() {
	int res = solve();
	printf("%d\n", res);
	return 0;
}