#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int M = 500'010;
vector<vector<int>> g(M);
vector<int> up(M), dp(M), day(M);
void dfs(int v) {
if(g[v].size()==0) dp[v] = 1;
for(auto u : g[v]) {
dfs(u);
dp[v] += dp[u];
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int d, n;
cin >> d >> n;
int nxt = 1;
for(int i=1; i<d; ++i) {
int l; cin >> l;
for(int j=0; j<l; ++j) {
day[nxt + n + j] = i;
int x; cin >> x;
if(x) {
--x;
g[nxt+x].push_back(nxt + n + j);
up[nxt+n+j]++;
}
}
nxt += n;
n = l;
}
vector<vector<int>> roots(d);
vector<vector<int>> leaves(d);
nxt += n;
for(int i=1; i<nxt; ++i) {
if(g[i].size()==0) {
leaves[day[i]].push_back(i);
}
if(up[i]==0) {
roots[day[i]].push_back(i);
dfs(i);
}
}
int free = 0;
int res = 0;
auto rekrutuj = [&]() {
if(free > 0) free--;
else res++;
};
auto zwolnij = [&]() {
free++;
};
for(int i=0; i<d; ++i) {
for(auto x : roots[i]) {
for(int j=0; j<dp[x]; ++j) rekrutuj();
}
for(auto x : leaves[i]) zwolnij();
}
cout << res << "\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 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> typedef long long ll; using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; int M = 500'010; vector<vector<int>> g(M); vector<int> up(M), dp(M), day(M); void dfs(int v) { if(g[v].size()==0) dp[v] = 1; for(auto u : g[v]) { dfs(u); dp[v] += dp[u]; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int d, n; cin >> d >> n; int nxt = 1; for(int i=1; i<d; ++i) { int l; cin >> l; for(int j=0; j<l; ++j) { day[nxt + n + j] = i; int x; cin >> x; if(x) { --x; g[nxt+x].push_back(nxt + n + j); up[nxt+n+j]++; } } nxt += n; n = l; } vector<vector<int>> roots(d); vector<vector<int>> leaves(d); nxt += n; for(int i=1; i<nxt; ++i) { if(g[i].size()==0) { leaves[day[i]].push_back(i); } if(up[i]==0) { roots[day[i]].push_back(i); dfs(i); } } int free = 0; int res = 0; auto rekrutuj = [&]() { if(free > 0) free--; else res++; }; auto zwolnij = [&]() { free++; }; for(int i=0; i<d; ++i) { for(auto x : roots[i]) { for(int j=0; j<dp[x]; ++j) rekrutuj(); } for(auto x : leaves[i]) zwolnij(); } cout << res << "\n"; return 0; } |
English