#include <iostream>
#include <vector>
#include <algorithm>
const int max_N = 500002;
int tree_starts[max_N];
int needed_staff[max_N];
struct Pos {
int tree;
bool is_leaf;
};
int main()
{
int k, n_1;
std::cin >> k >> n_1;
int day = 0;
int n_i = 0;
int pos = 0;
int tree_count = 0;
int prev;
std::vector<std::vector<Pos>> spotkania = std::vector<std::vector<Pos>>();
spotkania.push_back(std::vector<Pos>());
needed_staff[0] = 0;
for (int i = 0; i < n_1; i++) {
spotkania[0].push_back(Pos{ tree_count, true });
tree_starts[tree_count] = 0;
tree_count++;
}
for (day = 1; day < k; day++) {
//std::cout << day << "\n";
std::cin >> n_i;
needed_staff[day] = 0;
spotkania.push_back(std::vector<Pos>());
for (int p = 0; p < n_i; p++) {
std::cin >> prev;
if (prev == 0) {
spotkania[day].push_back({ tree_count, true });
tree_starts[tree_count] = day;
tree_count++;
}
else {
spotkania[day - 1][prev - 1].is_leaf = false;
int cur_tree = spotkania[day - 1][prev - 1].tree;
spotkania[day].push_back({ cur_tree, true });
}
}
}
int cur_staff = 0;
int max_staff = 0;
for (day = k-1; day >= 0; day--) {
for (auto v : spotkania[day]) {
if (v.is_leaf) {
needed_staff[tree_starts[v.tree]]++;
cur_staff++;
}
}
max_staff = std::max(max_staff, cur_staff);
cur_staff -= needed_staff[day];
}
std::cout << max_staff;
}
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 | #include <iostream> #include <vector> #include <algorithm> const int max_N = 500002; int tree_starts[max_N]; int needed_staff[max_N]; struct Pos { int tree; bool is_leaf; }; int main() { int k, n_1; std::cin >> k >> n_1; int day = 0; int n_i = 0; int pos = 0; int tree_count = 0; int prev; std::vector<std::vector<Pos>> spotkania = std::vector<std::vector<Pos>>(); spotkania.push_back(std::vector<Pos>()); needed_staff[0] = 0; for (int i = 0; i < n_1; i++) { spotkania[0].push_back(Pos{ tree_count, true }); tree_starts[tree_count] = 0; tree_count++; } for (day = 1; day < k; day++) { //std::cout << day << "\n"; std::cin >> n_i; needed_staff[day] = 0; spotkania.push_back(std::vector<Pos>()); for (int p = 0; p < n_i; p++) { std::cin >> prev; if (prev == 0) { spotkania[day].push_back({ tree_count, true }); tree_starts[tree_count] = day; tree_count++; } else { spotkania[day - 1][prev - 1].is_leaf = false; int cur_tree = spotkania[day - 1][prev - 1].tree; spotkania[day].push_back({ cur_tree, true }); } } } int cur_staff = 0; int max_staff = 0; for (day = k-1; day >= 0; day--) { for (auto v : spotkania[day]) { if (v.is_leaf) { needed_staff[tree_starts[v.tree]]++; cur_staff++; } } max_staff = std::max(max_staff, cur_staff); cur_staff -= needed_staff[day]; } std::cout << max_staff; } |
English