#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int k;
if (!(cin >> k)) return 0;
vector<int> n(k + 1);
vector<int> offset(k + 2, 0);
cin >> n[1];
offset[1] = 1;
int total_meetings = n[1];
vector<int> parent_idx(500005, 0);
for (int i = 2; i <= k; ++i) {
cin >> n[i];
offset[i] = total_meetings + 1;
for (int j = 0; j < n[i]; ++j) {
int p;
cin >> p;
if (p > 0) {
parent_idx[offset[i] + j] = offset[i - 1] + p - 1;
} else {
parent_idx[offset[i] + j] = 0;
}
}
total_meetings += n[i];
}
offset[k+1] = total_meetings + 1;
vector<long long> r(total_meetings + 1, 0);
for (int i = k; i >= 1; --i) {
for (int j = 0; j < n[i]; ++j) {
int curr_idx = offset[i] + j;
if (r[curr_idx] < 1) r[curr_idx] = 1;
int p_idx = parent_idx[curr_idx];
if (p_idx > 0) {
r[p_idx] += r[curr_idx];
}
}
}
long long max_employees = 0;
for (int i = 1; i <= k; ++i) {
long long current_day_sum = 0;
for (int j = 0; j < n[i]; ++j) {
current_day_sum += r[offset[i] + j];
}
if (current_day_sum > max_employees) {
max_employees = current_day_sum;
}
}
cout << max_employees << endl;
return 0;
}
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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int k; if (!(cin >> k)) return 0; vector<int> n(k + 1); vector<int> offset(k + 2, 0); cin >> n[1]; offset[1] = 1; int total_meetings = n[1]; vector<int> parent_idx(500005, 0); for (int i = 2; i <= k; ++i) { cin >> n[i]; offset[i] = total_meetings + 1; for (int j = 0; j < n[i]; ++j) { int p; cin >> p; if (p > 0) { parent_idx[offset[i] + j] = offset[i - 1] + p - 1; } else { parent_idx[offset[i] + j] = 0; } } total_meetings += n[i]; } offset[k+1] = total_meetings + 1; vector<long long> r(total_meetings + 1, 0); for (int i = k; i >= 1; --i) { for (int j = 0; j < n[i]; ++j) { int curr_idx = offset[i] + j; if (r[curr_idx] < 1) r[curr_idx] = 1; int p_idx = parent_idx[curr_idx]; if (p_idx > 0) { r[p_idx] += r[curr_idx]; } } } long long max_employees = 0; for (int i = 1; i <= k; ++i) { long long current_day_sum = 0; for (int j = 0; j < n[i]; ++j) { current_day_sum += r[offset[i] + j]; } if (current_day_sum > max_employees) { max_employees = current_day_sum; } } cout << max_employees << endl; return 0; } |
English