#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <string>
#include <numeric>
using namespace std;
using ll = long long;
const int MAX = 500001;
vector<ll> P[MAX], S[MAX];
int n, k;
int main() {
cin >> n >> k;
P[1].resize(k);
S[1].resize(k, 1);
for (int i = 2; i <= n; i++) {
int a;
cin >> a;
P[i].resize(a);
S[i].resize(a, 1);
for (auto &b : P[i]) {
cin >> b;
if (b) {
S[i-1][b-1] = 0;
}
}
}
ll res = 1;
for (int i = n; i >= 1; i--) {
ll t = accumulate(S[i].begin(), S[i].end(), 0ll);
for (int ind = 0; ind < P[i].size(); ind++) {
int a = P[i][ind];
if (a) {
S[i-1][a-1] += S[i][ind];
}
}
res = max(res, t);
}
cout << res << endl;
return 0;
}
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 | #include <iostream> #include <vector> #include <algorithm> #include <map> #include <set> #include <cmath> #include <string> #include <numeric> using namespace std; using ll = long long; const int MAX = 500001; vector<ll> P[MAX], S[MAX]; int n, k; int main() { cin >> n >> k; P[1].resize(k); S[1].resize(k, 1); for (int i = 2; i <= n; i++) { int a; cin >> a; P[i].resize(a); S[i].resize(a, 1); for (auto &b : P[i]) { cin >> b; if (b) { S[i-1][b-1] = 0; } } } ll res = 1; for (int i = n; i >= 1; i--) { ll t = accumulate(S[i].begin(), S[i].end(), 0ll); for (int ind = 0; ind < P[i].size(); ind++) { int a = P[i][ind]; if (a) { S[i-1][a-1] += S[i][ind]; } } res = max(res, t); } cout << res << endl; return 0; } |
English