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

using namespace std;

const int maxN=5e5+10;

int n, k, vals1[maxN]{0}, vals2[maxN]{0}, result, s[maxN];
int** arr = new int*[maxN];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin>>k>>n;
    for(int i=0; i<k-1; i++){
        int x;
        cin>>x;
        arr[i+1] = new int[x];
        s[i+1] = x;
        for(int j=0; j<x; j++){
            cin>>arr[i+1][j];
        }
    }
    arr[0] = new int[n];
    s[0] = n;
    for(int i=0; i<s[k-1]; i++)vals1[i]=1;
    result = s[k-1];

    //return 0;

    for(int i=k-1; i>0; i--){
        int S=0;
        for(int j=0; j<s[i]; j++){
            if(arr[i][j] > 0)vals2[arr[i][j]-1] += vals1[j];
            vals1[j] = 0;
        }
        for(int j=0; j<s[i-1]; j++){
            vals2[j] = max(vals2[j], 1);
            S += vals2[j];
        }
        result = max(result, S);
        swap(vals1, vals2);
    }
    cout<<result;
    return 0;
}