#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define BOOST ios_base::sync_with_stdio(0), cin.tie(0)
template<typename T>
using vc = vector<T>;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
const int N = 1e6 + 5;
vc<int> t[N];
int notlf[N];
int lfcnt[N];
int p[N];
int n = 0;
int main(){
BOOST;
int k, n1;
cin >> k >> n1;
t[0] = vc<int>(n1);
for(int i=0; i<n1; i++){
t[0][i] = ++n;
}
for(int i=1; i<k; i++){
int ni; cin >> ni;
t[i] = vc<int>(ni);
int n0 = n - (int)t[i-1].size();
for(int j=0; j<ni; j++){
t[i][j] = ++n;
int idx; cin >> idx;
if(idx){
p[n] = n0 + idx;
notlf[p[n]] = 1;
}
}
}
for(int i=n; i>=1; i--){
if(!notlf[i]) lfcnt[i] = 1;
lfcnt[p[i]] += lfcnt[i];
}
int cur = 0;
for(int i=0; i<k; i++){
int lc = 0;
for(auto x : t[i]){
if(p[x] == 0) cur -= lfcnt[x];
if(!notlf[x]) lc++;
}
cur = max(cur, 0);
cur += lc;
}
cout << cur << "\n";
}
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 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second #define pb push_back #define all(x) (x).begin(), (x).end() #define BOOST ios_base::sync_with_stdio(0), cin.tie(0) template<typename T> using vc = vector<T>; using ll = long long; using ld = long double; using ii = pair<int, int>; const int N = 1e6 + 5; vc<int> t[N]; int notlf[N]; int lfcnt[N]; int p[N]; int n = 0; int main(){ BOOST; int k, n1; cin >> k >> n1; t[0] = vc<int>(n1); for(int i=0; i<n1; i++){ t[0][i] = ++n; } for(int i=1; i<k; i++){ int ni; cin >> ni; t[i] = vc<int>(ni); int n0 = n - (int)t[i-1].size(); for(int j=0; j<ni; j++){ t[i][j] = ++n; int idx; cin >> idx; if(idx){ p[n] = n0 + idx; notlf[p[n]] = 1; } } } for(int i=n; i>=1; i--){ if(!notlf[i]) lfcnt[i] = 1; lfcnt[p[i]] += lfcnt[i]; } int cur = 0; for(int i=0; i<k; i++){ int lc = 0; for(auto x : t[i]){ if(p[x] == 0) cur -= lfcnt[x]; if(!notlf[x]) lc++; } cur = max(cur, 0); cur += lc; } cout << cur << "\n"; } |
English