#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef vector<LL> VI;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int k, n1;
cin >> k >> n1;
VI dayStart(k + 1, 0);
VI dayCount(k + 1, 0);
dayCount[1] = n1;
dayStart[1] = 0;
int total = n1;
vector<VI> parentLocal(k + 1);
parentLocal[1] = VI(n1, 0);
for (int day = 2; day <= k; ++day)
{
int ni = 0;
cin >> ni;
dayCount[day] = ni;
parentLocal[day].resize(ni);
for (int j = 0; j < ni; ++j) cin >> parentLocal[day][j];
total += ni;
}
for (int day = 2; day <= k; ++day) dayStart[day] = dayStart[day - 1] + dayCount[day - 1];
VI parent(total, -1);
for (int j = 0; j < dayCount[1]; ++j) parent[dayStart[1] + j] = -1;
for (int day = 2; day <= k; ++day)
{
int prevStart = dayStart[day - 1];
int curStart = dayStart[day];
for (int j = 0; j < dayCount[day]; ++j)
{
int a = parentLocal[day][j];
if (a==0) parent[curStart + j] = -1;
else parent[curStart + j] = prevStart + (a - 1);
}
}
//Bottom-up
VI childSum(total, 0);
VI f(total, 0);
LL answer = 0;
for (int day = k; day >= 1; --day)
{
int start = dayStart[day];
int cnt = dayCount[day];
LL daySum = 0LL;
for (int j = 0; j < cnt; ++j)
{
int v = start + j;
f[v] = childSum[v] == 0 ? 1LL : childSum[v];
daySum += f[v];
}
answer = max(answer, daySum);
for (int j= 0; j < cnt; ++j)
{
int v = start + j;
int p = parent[v];
if (p != -1) childSum[p] += f[v];
}
}
cout << answer << 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 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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long LL; typedef vector<LL> VI; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int k, n1; cin >> k >> n1; VI dayStart(k + 1, 0); VI dayCount(k + 1, 0); dayCount[1] = n1; dayStart[1] = 0; int total = n1; vector<VI> parentLocal(k + 1); parentLocal[1] = VI(n1, 0); for (int day = 2; day <= k; ++day) { int ni = 0; cin >> ni; dayCount[day] = ni; parentLocal[day].resize(ni); for (int j = 0; j < ni; ++j) cin >> parentLocal[day][j]; total += ni; } for (int day = 2; day <= k; ++day) dayStart[day] = dayStart[day - 1] + dayCount[day - 1]; VI parent(total, -1); for (int j = 0; j < dayCount[1]; ++j) parent[dayStart[1] + j] = -1; for (int day = 2; day <= k; ++day) { int prevStart = dayStart[day - 1]; int curStart = dayStart[day]; for (int j = 0; j < dayCount[day]; ++j) { int a = parentLocal[day][j]; if (a==0) parent[curStart + j] = -1; else parent[curStart + j] = prevStart + (a - 1); } } //Bottom-up VI childSum(total, 0); VI f(total, 0); LL answer = 0; for (int day = k; day >= 1; --day) { int start = dayStart[day]; int cnt = dayCount[day]; LL daySum = 0LL; for (int j = 0; j < cnt; ++j) { int v = start + j; f[v] = childSum[v] == 0 ? 1LL : childSum[v]; daySum += f[v]; } answer = max(answer, daySum); for (int j= 0; j < cnt; ++j) { int v = start + j; int p = parent[v]; if (p != -1) childSum[p] += f[v]; } } cout << answer << endl; return 0; } |
English