#include <bits/stdc++.h> #include <random> #define ll long long int #define pb push_back #define st first #define nd second #define pii pair<int,int> #define mp make_pair #define pll pair<long long,long long> #define ld long double #define ull unsigned long long using namespace std; const int nax = 2e5 + 5; vector<int> adj[nax]; vector<int> doMnie[nax]; int n; int outdeg[nax]; ll ans[nax]; void solve(){ cin >> n; for(int i=1;i<=n;i++){ int x; cin >> x; for(int j=1;j<=x;j++){ int w; cin >> w; adj[i].pb(w); doMnie[w].pb(i); } } for(int b=1;b<=n;b++){ for(int j=1;j<=n;j++){ outdeg[j] = adj[j].size(); } vector<int> Q; Q.pb(b); int wsk = 0; vector<bool> waiting(n + 1, false); while(wsk < Q.size()){ int cur = Q[wsk++]; for(int x : doMnie[cur]){ outdeg[x] -= 1; if(x != b && outdeg[x] == 0){ waiting[x] = true; } } } for(int i=1;i<=n;i++){ if(waiting[i] == true){ waiting[i] = false; Q.pb(i); } while(wsk < Q.size()){ int cur = Q[wsk++]; for(int x : doMnie[cur]){ outdeg[x] -= 1; if(x != b && outdeg[x] == 0){ if(x <= i) Q.pb(x); else waiting[x] = true; } } } if(i >= b) ans[i] += Q.size() - 1; } } for(int i=1;i<=n;i++) cout << ans[i] << " "; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int tt = 1; // cin >> tt; while(tt--) solve(); 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 | #include <bits/stdc++.h> #include <random> #define ll long long int #define pb push_back #define st first #define nd second #define pii pair<int,int> #define mp make_pair #define pll pair<long long,long long> #define ld long double #define ull unsigned long long using namespace std; const int nax = 2e5 + 5; vector<int> adj[nax]; vector<int> doMnie[nax]; int n; int outdeg[nax]; ll ans[nax]; void solve(){ cin >> n; for(int i=1;i<=n;i++){ int x; cin >> x; for(int j=1;j<=x;j++){ int w; cin >> w; adj[i].pb(w); doMnie[w].pb(i); } } for(int b=1;b<=n;b++){ for(int j=1;j<=n;j++){ outdeg[j] = adj[j].size(); } vector<int> Q; Q.pb(b); int wsk = 0; vector<bool> waiting(n + 1, false); while(wsk < Q.size()){ int cur = Q[wsk++]; for(int x : doMnie[cur]){ outdeg[x] -= 1; if(x != b && outdeg[x] == 0){ waiting[x] = true; } } } for(int i=1;i<=n;i++){ if(waiting[i] == true){ waiting[i] = false; Q.pb(i); } while(wsk < Q.size()){ int cur = Q[wsk++]; for(int x : doMnie[cur]){ outdeg[x] -= 1; if(x != b && outdeg[x] == 0){ if(x <= i) Q.pb(x); else waiting[x] = true; } } } if(i >= b) ans[i] += Q.size() - 1; } } for(int i=1;i<=n;i++) cout << ans[i] << " "; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int tt = 1; // cin >> tt; while(tt--) solve(); return 0; } |