#include <bits/stdc++.h>
#define FOR(i,p,k) for(int i=(p);i<=(k);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define RFOR(i,p,n) for(int i=(p);i>=(n);--i)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define ssize(x) int((x).size())
#define fi first
#define se second
#define V vector
#define pb push_back
#define eb emplace_back
#define C const
#define gc getchar_unlocked
#define pc putchar_unlocked
#define pn putchar_unlocked('\n')
using namespace std;
typedef long long ll;
typedef __int128_t lll;
typedef V<int> vi;
typedef V<ll> vll;
typedef const int ci;
typedef const ll cll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
void chmin(auto &a, auto b){a=min(a,b);}
void chmax(auto &a, auto b){a=max(a,b);}
ci inf = 2.1e9;
cll infll = 4.5e18;
int I(){
int z;
int c = gc();
while(c < '0' || c > '9') c = gc();
for(z = 0; c >= '0' && c <= '9'; c = gc()) z = 10*z+c-'0';
return z;
}
void O(int a){
static int tab[20];
int it = 0;
do{
tab[it++] = int('0'+a%10);
} while(a /= 10);
while(it) pc(tab[--it]);
}
void answer(){
ci k = I();
vi n(k+1);
V<vi> wej(k+1), dp(k+1);
FOR(i, 1, k){
n[i] = I();
wej[i].resize(n[i]+1, 0);
dp[i].resize(n[i]+1, 0);
if(i > 1) FOR(j, 1, n[i]) wej[i][j] = I();
}
int wyn = 0;
RFOR(i, k, 1){
if(i+1 <= k) FOR(j, 1, n[i+1]) dp[i][wej[i+1][j]] += dp[i+1][j];
FOR(j, 1, n[i]) chmax(dp[i][j], 1);
int suma = 0;
FOR(j, 1, n[i]) suma += dp[i][j];
chmax(wyn, suma);
}
O(wyn), pn;
}
int main(){
int tt = 1;
while(tt--) answer();
}
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 | #include <bits/stdc++.h> #define FOR(i,p,k) for(int i=(p);i<=(k);++i) #define REP(i,n) FOR(i,0,(n)-1) #define RFOR(i,p,n) for(int i=(p);i>=(n);--i) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define ssize(x) int((x).size()) #define fi first #define se second #define V vector #define pb push_back #define eb emplace_back #define C const #define gc getchar_unlocked #define pc putchar_unlocked #define pn putchar_unlocked('\n') using namespace std; typedef long long ll; typedef __int128_t lll; typedef V<int> vi; typedef V<ll> vll; typedef const int ci; typedef const ll cll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ll, int> pli; typedef pair<int, ll> pil; void chmin(auto &a, auto b){a=min(a,b);} void chmax(auto &a, auto b){a=max(a,b);} ci inf = 2.1e9; cll infll = 4.5e18; int I(){ int z; int c = gc(); while(c < '0' || c > '9') c = gc(); for(z = 0; c >= '0' && c <= '9'; c = gc()) z = 10*z+c-'0'; return z; } void O(int a){ static int tab[20]; int it = 0; do{ tab[it++] = int('0'+a%10); } while(a /= 10); while(it) pc(tab[--it]); } void answer(){ ci k = I(); vi n(k+1); V<vi> wej(k+1), dp(k+1); FOR(i, 1, k){ n[i] = I(); wej[i].resize(n[i]+1, 0); dp[i].resize(n[i]+1, 0); if(i > 1) FOR(j, 1, n[i]) wej[i][j] = I(); } int wyn = 0; RFOR(i, k, 1){ if(i+1 <= k) FOR(j, 1, n[i+1]) dp[i][wej[i+1][j]] += dp[i+1][j]; FOR(j, 1, n[i]) chmax(dp[i][j], 1); int suma = 0; FOR(j, 1, n[i]) suma += dp[i][j]; chmax(wyn, suma); } O(wyn), pn; } int main(){ int tt = 1; while(tt--) answer(); } |
English