#include <bits/stdc++.h>
using namespace std;
#define int long long
int graph[1000009];
vector<int> last[1000009];
int dp[1000009];
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int k,n;
cin>>k>>n;
int f = n+1;
for (int i = 1; i<=n;i++) {
last[1].push_back(i);
}
for (int i = 1; i<k; i++) {
int x;
cin>>x;
for (int j = 0; j<x; j++) {
int q;
cin>>q;
if (q != 0) {
graph[f] = (last[i][q-1]);
}
last[i+1].push_back(f);
f++;
}
}
int ans = 0;
for (int i = k; i>= 1; i--) {
int g = 0;
for (auto x: last[i]) {
if (dp[x] == 0) {
dp[x] = 1;
}
dp[graph[x]] += dp[x];
g+= dp[x];
}
ans=max(ans,g);
}
cout << ans;
}
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 | #include <bits/stdc++.h> using namespace std; #define int long long int graph[1000009]; vector<int> last[1000009]; int dp[1000009]; int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int k,n; cin>>k>>n; int f = n+1; for (int i = 1; i<=n;i++) { last[1].push_back(i); } for (int i = 1; i<k; i++) { int x; cin>>x; for (int j = 0; j<x; j++) { int q; cin>>q; if (q != 0) { graph[f] = (last[i][q-1]); } last[i+1].push_back(f); f++; } } int ans = 0; for (int i = k; i>= 1; i--) { int g = 0; for (auto x: last[i]) { if (dp[x] == 0) { dp[x] = 1; } dp[graph[x]] += dp[x]; g+= dp[x]; } ans=max(ans,g); } cout << ans; } |
English