#include<bits/stdc++.h>
#define VAR(i,n) __typeof(n) i = (n)
#define loop(i,j,s) for(int i=j;i<s;i++)
#define loopback(i,j,s) for(int i=j;i>=s;i--)
#define foreach(i,c) for(VAR(i,(c).begin());i!=(c).end();i++)
#define pln( x ) cout << x << "\n"
#define ps( x ) cout << x << " "
#define entr cout << "\n"
#define pcnt(i) __builtin_popcount(i)
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define SIZE(c) (c).size()
#define ALL(c) (c).begin(), (c).end()
using namespace std;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef vector<vector<int> > VVI;
const int INFTY=20000000;
const int MAX=500100;
const int MOD=10000000;
void coutTab(int* tab,int n){
loop(i,0,n){
cout<<tab[i]<<" ";
}
cout<<"\n";
}
template<class T> void coutVec(vector<T> tab){
for(T t : tab){
cout<<t<<" ";
}
cout<<"\n";
}
//------------------------------------------
int N, k;
VVI G(MAX);
VVI D(MAX); // sessions per day
int n[MAX];
int dp[MAX];
void coutG(VVI G){
for(int i=0;i<N;i++){
cout<<i<<": ";
coutVec(G[i]);
}
cout<<"\n";
}
int solve(){
// count how many needs to be on this session to cover all continuations later
for(int i=N-1;i>=0;i--) {
if(SIZE(G[i])==0) dp[i]=1;
else {
dp[i]=0;
foreach(j, G[i]) { dp[i] += dp[*j]; }
}
}
// coutTab(dp, N);
// go over days
int maxi=0;
for(int d=k-1;d>=0;d--){
int ss=0;
for(int i=0;i<SIZE(D[d]);i++){ ss+=dp[D[d][i]]; }
maxi = max(maxi, ss);
}
return maxi;
}
int main(){
ios_base::sync_with_stdio(0);
cin>>k>>n[0];
int Sn=0;
int a;
for(int i=0;i<n[0];i++) D[0].pb(i);
for(int i=1;i<k;i++) {
cin>>n[i];
for(int j=0;j<n[i];j++){
cin>>a;
if(a != 0) {
G[Sn+a-1].pb(Sn+n[i-1]+j);
}
D[i].pb(Sn+n[i-1]+j);
}
Sn += n[i-1];
}
N = Sn + n[k-1];
// coutG(G);
// coutG(GT);
pln(solve());
}
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | #include<bits/stdc++.h> #define VAR(i,n) __typeof(n) i = (n) #define loop(i,j,s) for(int i=j;i<s;i++) #define loopback(i,j,s) for(int i=j;i>=s;i--) #define foreach(i,c) for(VAR(i,(c).begin());i!=(c).end();i++) #define pln( x ) cout << x << "\n" #define ps( x ) cout << x << " " #define entr cout << "\n" #define pcnt(i) __builtin_popcount(i) #define ll long long #define pb push_back #define mp make_pair #define ff first #define ss second #define SIZE(c) (c).size() #define ALL(c) (c).begin(), (c).end() using namespace std; typedef vector<int> VI; typedef pair<int, int> PII; typedef vector<vector<int> > VVI; const int INFTY=20000000; const int MAX=500100; const int MOD=10000000; void coutTab(int* tab,int n){ loop(i,0,n){ cout<<tab[i]<<" "; } cout<<"\n"; } template<class T> void coutVec(vector<T> tab){ for(T t : tab){ cout<<t<<" "; } cout<<"\n"; } //------------------------------------------ int N, k; VVI G(MAX); VVI D(MAX); // sessions per day int n[MAX]; int dp[MAX]; void coutG(VVI G){ for(int i=0;i<N;i++){ cout<<i<<": "; coutVec(G[i]); } cout<<"\n"; } int solve(){ // count how many needs to be on this session to cover all continuations later for(int i=N-1;i>=0;i--) { if(SIZE(G[i])==0) dp[i]=1; else { dp[i]=0; foreach(j, G[i]) { dp[i] += dp[*j]; } } } // coutTab(dp, N); // go over days int maxi=0; for(int d=k-1;d>=0;d--){ int ss=0; for(int i=0;i<SIZE(D[d]);i++){ ss+=dp[D[d][i]]; } maxi = max(maxi, ss); } return maxi; } int main(){ ios_base::sync_with_stdio(0); cin>>k>>n[0]; int Sn=0; int a; for(int i=0;i<n[0];i++) D[0].pb(i); for(int i=1;i<k;i++) { cin>>n[i]; for(int j=0;j<n[i];j++){ cin>>a; if(a != 0) { G[Sn+a-1].pb(Sn+n[i-1]+j); } D[i].pb(Sn+n[i-1]+j); } Sn += n[i-1]; } N = Sn + n[k-1]; // coutG(G); // coutG(GT); pln(solve()); } |
English