#include "bits/stdc++.h"
using namespace std;
#define int long long
#define ll long long
#define ld long double
#define endl '\n'
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define all(x) begin(x),end(x)
#define FOR(i,l,r) for(int i=(l);i<=(r);i++)
#define ROF(i,r,l) for(int i=(r);i>=(l);i--)
#define _$ auto operator<<(auto&o,auto x)->decltype
_$(x.st,o){return o<<"("<<x.st<<", "<<x.nd<<")";}
_$(end(x),o){o<<"{";for(int i=0;auto e:x)o<<","+!i++<<e;return o<<"}";}
#ifdef LOCAL
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){\
((cerr<<$<<"; "),...)<<'\n';}(x)
#else
#define debug(...)
#endif
#define rep(i,a,b) for(int i=(a);i<(b); i++)
using pii=pair<int,int>;
using vi=vector<int>;
const int inf = 1e9+7;
signed main() {
cin.tie(0)->sync_with_stdio(0);
int k, n1;
cin >> k >> n1;
vector<vi> a(k+1), vis(k+1), last(k+1);
a[1].resize(n1+1);
vis[1].resize(n1+1);
last[1].resize(n1+1);
FOR(i,2,k){
int ni;
cin >> ni;
a[i].resize(ni+1);
vis[i].resize(ni+1);
last[i].resize(ni+1);
FOR(j,1,ni) {
cin >> a[i][j];
if (a[i][j] == 0) {
last[i][j] = i - 1;
} else {
last[i][j] = last[i-1][a[i][j]];
}
}
}
vi add(k+1);
int now = 0, ans = 0;
ROF(i,k,1){
now += add[i];
rep(j,1,sz(a[i])) if (!vis[i][j]) {
int ii = i, jj = j;
while (ii >= 1 && jj >= 1 && !vis[ii][jj]) {
vis[ii][jj] = true;
debug(i, j, ii, jj);
jj = a[ii][jj];
ii--;
}
add[last[i][j]]++;
if (now > 0) {
now--;
} else {
debug("+", i,j);
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 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 86 87 88 89 90 | #include "bits/stdc++.h" using namespace std; #define int long long #define ll long long #define ld long double #define endl '\n' #define st first #define nd second #define mp make_pair #define pb push_back #define eb emplace_back #define sz(x) (int)(x).size() #define all(x) begin(x),end(x) #define FOR(i,l,r) for(int i=(l);i<=(r);i++) #define ROF(i,r,l) for(int i=(r);i>=(l);i--) #define _$ auto operator<<(auto&o,auto x)->decltype _$(x.st,o){return o<<"("<<x.st<<", "<<x.nd<<")";} _$(end(x),o){o<<"{";for(int i=0;auto e:x)o<<","+!i++<<e;return o<<"}";} #ifdef LOCAL #define debug(x...) cerr<<"["#x"]: ",[](auto...$){\ ((cerr<<$<<"; "),...)<<'\n';}(x) #else #define debug(...) #endif #define rep(i,a,b) for(int i=(a);i<(b); i++) using pii=pair<int,int>; using vi=vector<int>; const int inf = 1e9+7; signed main() { cin.tie(0)->sync_with_stdio(0); int k, n1; cin >> k >> n1; vector<vi> a(k+1), vis(k+1), last(k+1); a[1].resize(n1+1); vis[1].resize(n1+1); last[1].resize(n1+1); FOR(i,2,k){ int ni; cin >> ni; a[i].resize(ni+1); vis[i].resize(ni+1); last[i].resize(ni+1); FOR(j,1,ni) { cin >> a[i][j]; if (a[i][j] == 0) { last[i][j] = i - 1; } else { last[i][j] = last[i-1][a[i][j]]; } } } vi add(k+1); int now = 0, ans = 0; ROF(i,k,1){ now += add[i]; rep(j,1,sz(a[i])) if (!vis[i][j]) { int ii = i, jj = j; while (ii >= 1 && jj >= 1 && !vis[ii][jj]) { vis[ii][jj] = true; debug(i, j, ii, jj); jj = a[ii][jj]; ii--; } add[last[i][j]]++; if (now > 0) { now--; } else { debug("+", i,j); ans++; } } } cout << ans << '\n'; return 0; } |
English