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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Spotkania {
    int start_day;
};

class konferencjaSolver {
    int total_days;
    int first_day_meetings;
    vector<int> diff_array;
    vector<Spotkania> yesterday_paths;

    void registerAgentInterval(int start_day, int end_day) {
        diff_array[start_day]++;
        diff_array[end_day + 1]--;
    }

public:
    konferencjaSolver(int k, int n1) : total_days(k), first_day_meetings(n1) {
        diff_array.resize(k + 2, 0);
        yesterday_paths.assign(n1 + 1, {1});
    }

    void agenciKrolaBajtocji() {
        for (int today = 2; today <= total_days; ++today) {
            int todays_meetings_count;
            cin >> todays_meetings_count;

            vector<int> parent_meeting(todays_meetings_count + 1);
            vector<int> continuation_count(yesterday_paths.size(), 0);

            for (int j = 1; j <= todays_meetings_count; ++j) {
                cin >> parent_meeting[j];
                if (parent_meeting[j] > 0) {
                    continuation_count[parent_meeting[j]]++;
                }
            }
            for (size_t j = 1; j < yesterday_paths.size(); ++j) {
                if (continuation_count[j] == 0) {
                    registerAgentInterval(yesterday_paths[j].start_day, today - 1);
                }
            }

            vector<Spotkania> todays_paths(todays_meetings_count + 1);
            for (int j = 1; j <= todays_meetings_count; ++j) {
                if (parent_meeting[j] == 0) {
                    todays_paths[j] = {today};
                } else {
                    todays_paths[j] = {yesterday_paths[parent_meeting[j]].start_day};
                }
            }

            yesterday_paths = move(todays_paths);
        }

        for (size_t j = 1; j < yesterday_paths.size(); ++j) {
            registerAgentInterval(yesterday_paths[j].start_day, total_days);
        }
    }

    int podliczNiezbednychAgentow() const {
        int max_agents = 0;
        int current_agents = 0;

        for (int i = 1; i <= total_days; ++i) {
            current_agents += diff_array[i];
            max_agents = max(max_agents, current_agents);
        }

        return max_agents;
    }
};

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

    int n1;
    if (int k; cin >> k >> n1) {
        konferencjaSolver solver(k, n1);

        solver.agenciKrolaBajtocji();

        cout << solver.podliczNiezbednychAgentow() << endl;
    }

    return 0;
}