#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, x;
cin >> n >> x;
vector<int> W(n + 1);
W[1] = x;
vector<vector<int>> saved_curr(n + 1);
for (int i = 2; i <= n; i++) {
int w;
cin >> w;
W[i] = w;
saved_curr[i].resize(w + 1);
for (int k = 1; k <= w; k++) {
cin >> saved_curr[i][k];
}
}
vector<long long> zlicz_prev(W[n] + 1, 1);
long long ans = W[n];
for (int i = n - 1; i >= 1; i--) {
int w = W[i];
vector<long long> zlicz_curr(w + 1, 0);
for (int k = 1; k <= W[i + 1]; k++) {
int parent = saved_curr[i + 1][k];
if (parent > 0) {
zlicz_curr[parent] += zlicz_prev[k];
}
}
long long temp = 0;
for (int k = 1; k <= w; k++) {
if (zlicz_curr[k] < 1) zlicz_curr[k] = 1;
temp += zlicz_curr[k];
}
ans = max(ans, temp);
zlicz_prev = zlicz_curr;
}
cout << ans << 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 | #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, x; cin >> n >> x; vector<int> W(n + 1); W[1] = x; vector<vector<int>> saved_curr(n + 1); for (int i = 2; i <= n; i++) { int w; cin >> w; W[i] = w; saved_curr[i].resize(w + 1); for (int k = 1; k <= w; k++) { cin >> saved_curr[i][k]; } } vector<long long> zlicz_prev(W[n] + 1, 1); long long ans = W[n]; for (int i = n - 1; i >= 1; i--) { int w = W[i]; vector<long long> zlicz_curr(w + 1, 0); for (int k = 1; k <= W[i + 1]; k++) { int parent = saved_curr[i + 1][k]; if (parent > 0) { zlicz_curr[parent] += zlicz_prev[k]; } } long long temp = 0; for (int k = 1; k <= w; k++) { if (zlicz_curr[k] < 1) zlicz_curr[k] = 1; temp += zlicz_curr[k]; } ans = max(ans, temp); zlicz_prev = zlicz_curr; } cout << ans << endl; return 0; } |
English