#include <bits/stdc++.h>
using namespace std;
#define rep(a, b) for (int a = 0; a < (b); a++)
#define rep1(a, b) for (int a = 1; a <= (b); a++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int MOD = 1e9 + 7;
const int LIM = 5e5 + 7;
int k;
vector<int> where[LIM];
vector<int> counts[LIM];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int cnt;
cin >> k >> cnt;
where[0] = vector<int>(cnt+1, 0);
counts[0] = vector<int>(cnt+1, 0);
rep1(i, k-1) {
cin >> cnt;
where[i] = vector<int>(cnt+1, 0);
counts[i] = vector<int>(cnt+1, 0);
rep1(j, cnt) cin >> where[i][j];
}
rep1(i, (int)counts[k-1].size()-1) counts[k-1][i] = 1;
int ans = (int)counts[k-1].size()-1;
for (int d = k-1; d > 0; d--) {
rep(i, counts[d].size()) counts[d-1][where[d][i]] += counts[d][i];
rep1(i, (int)counts[d-1].size()-1) if (counts[d-1][i] == 0) {
counts[d-1][i] = 1;
if (counts[d-1][0] > 0) counts[d-1][0]--;
else ans++;
}
}
cout << ans << "\n";
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 | #include <bits/stdc++.h> using namespace std; #define rep(a, b) for (int a = 0; a < (b); a++) #define rep1(a, b) for (int a = 1; a <= (b); a++) #define all(x) (x).begin(), (x).end() using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; const int MOD = 1e9 + 7; const int LIM = 5e5 + 7; int k; vector<int> where[LIM]; vector<int> counts[LIM]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int cnt; cin >> k >> cnt; where[0] = vector<int>(cnt+1, 0); counts[0] = vector<int>(cnt+1, 0); rep1(i, k-1) { cin >> cnt; where[i] = vector<int>(cnt+1, 0); counts[i] = vector<int>(cnt+1, 0); rep1(j, cnt) cin >> where[i][j]; } rep1(i, (int)counts[k-1].size()-1) counts[k-1][i] = 1; int ans = (int)counts[k-1].size()-1; for (int d = k-1; d > 0; d--) { rep(i, counts[d].size()) counts[d-1][where[d][i]] += counts[d][i]; rep1(i, (int)counts[d-1].size()-1) if (counts[d-1][i] == 0) { counts[d-1][i] = 1; if (counts[d-1][0] > 0) counts[d-1][0]--; else ans++; } } cout << ans << "\n"; return 0; } |
English