#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
struct debug {
template <class c>
debug& operator<<(const c&) { return *this; }
};
#define imie(...) ""
#endif
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
int R(int a, int b) {
static mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
return uniform_int_distribution<int>(a, b)(rng);
}
constexpr int ALPHA = 26;
void solve(int tc) {
int k;
cin >> k;
vector n(k, 0);
vector<vector<int>> ojciec(k), synow(k), lisci(k);
for (int i = 0; i < k; i++) {
cin >> n[i];
ojciec[i] = synow[i] = lisci[i] = vector<int>(n[i], 0);
for (auto &o : ojciec[i]) {
if (i > 0) {
cin >> o;
}
o--;
if (o >= 0) synow[i-1][o]++;
}
}
vector<int> pref(k+1, 0);
for (int i = k-1; i >= 0; i--) {
for (int j = 0; j < n[i]; j++) {
if (synow[i][j] == 0) {
lisci[i][j] = 1;
pref[i + 1]--;
}
if (ojciec[i][j] >= 0) {
lisci[i-1][ojciec[i][j]] += lisci[i][j];
} else {
pref[i] += lisci[i][j];
}
}
}
debug() << imie(pref);
int wynik = 0;
for (int i = 0; i <= k; i++) {
if (i > 0) pref[i] += pref[i-1];
wynik = max(wynik, pref[i]);
}
cout << wynik << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tests = 1;
debug() << imie(tests);
for (int tc = 1; tc <= tests; tc++) {
solve(tc);
}
}
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 | #include <bits/stdc++.h> #define all(x) (x).begin(), (x).end() using namespace std; #ifdef LOCAL #include "debug.h" #else struct debug { template <class c> debug& operator<<(const c&) { return *this; } }; #define imie(...) "" #endif using ll = long long; using ld = long double; using pii = pair<int, int>; int R(int a, int b) { static mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); return uniform_int_distribution<int>(a, b)(rng); } constexpr int ALPHA = 26; void solve(int tc) { int k; cin >> k; vector n(k, 0); vector<vector<int>> ojciec(k), synow(k), lisci(k); for (int i = 0; i < k; i++) { cin >> n[i]; ojciec[i] = synow[i] = lisci[i] = vector<int>(n[i], 0); for (auto &o : ojciec[i]) { if (i > 0) { cin >> o; } o--; if (o >= 0) synow[i-1][o]++; } } vector<int> pref(k+1, 0); for (int i = k-1; i >= 0; i--) { for (int j = 0; j < n[i]; j++) { if (synow[i][j] == 0) { lisci[i][j] = 1; pref[i + 1]--; } if (ojciec[i][j] >= 0) { lisci[i-1][ojciec[i][j]] += lisci[i][j]; } else { pref[i] += lisci[i][j]; } } } debug() << imie(pref); int wynik = 0; for (int i = 0; i <= k; i++) { if (i > 0) pref[i] += pref[i-1]; wynik = max(wynik, pref[i]); } cout << wynik << "\n"; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tests = 1; debug() << imie(tests); for (int tc = 1; tc <= tests; tc++) { solve(tc); } } |
English