#include<iostream>
using namespace std;
using ll = long long;
constexpr int MAXN = 5e6+11;
ll cnt[MAXN], dp[MAXN];
int adj[MAXN];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int k{}, n{};
cin >> k >> n;
int sumW{};
cnt[0] = n;
for(int i = 1; i <= k-1; ++i) {
cin >> cnt[i];
for(int j = 1; j <= cnt[i]; ++j) {
int a{};
cin >> a;
if(a) adj[sumW+cnt[i-1]+j] = sumW+a;
}
sumW += cnt[i-1];
}
sumW += cnt[k-1];
ll ans{};
for(int i = k - 1; i >= 0; --i) {
sumW -= cnt[i];
ll cur{};
for(int j = 1; j <= cnt[i]; ++j) {
dp[sumW+j] = max(dp[sumW+j], 1ll);
cur += dp[sumW+j];
if(adj[sumW+j] != 0) {
dp[adj[sumW+j]] += dp[sumW+j];
}
}
ans = max(ans, cur);
}
cout << ans << '\n';
}
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 | #include<iostream> using namespace std; using ll = long long; constexpr int MAXN = 5e6+11; ll cnt[MAXN], dp[MAXN]; int adj[MAXN]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int k{}, n{}; cin >> k >> n; int sumW{}; cnt[0] = n; for(int i = 1; i <= k-1; ++i) { cin >> cnt[i]; for(int j = 1; j <= cnt[i]; ++j) { int a{}; cin >> a; if(a) adj[sumW+cnt[i-1]+j] = sumW+a; } sumW += cnt[i-1]; } sumW += cnt[k-1]; ll ans{}; for(int i = k - 1; i >= 0; --i) { sumW -= cnt[i]; ll cur{}; for(int j = 1; j <= cnt[i]; ++j) { dp[sumW+j] = max(dp[sumW+j], 1ll); cur += dp[sumW+j]; if(adj[sumW+j] != 0) { dp[adj[sumW+j]] += dp[sumW+j]; } } ans = max(ans, cur); } cout << ans << '\n'; } |
English