#define _USE_MATH_DEFINES #include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using Vi = vector<int>; using Pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(12); int n; cin >> n; vector<Vi> inv(n); Vi deg(n); rep(i, 0, n) { int k; cin >> k; while (k--) { int j; cin >> j; j--; inv[j].pb(i); deg[i]++; } } vector<ll> ans(n, -1); Vi vis, que, lvl(n, -1), cur = deg; que.reserve(n+5); vis.reserve(n+5); rep(b, 0, n) { lvl[b] = b; cur[b] = 0; que.pb(b); vis.pb(b); rep(pos, 0, sz(que)) { int v = que[pos]; ans[lvl[v]]++; each(e, inv[v]) { if (lvl[e] == -1) { lvl[e] = e; cur[e] = deg[e]; vis.pb(e); } lvl[e] = max(lvl[e], lvl[v]); if (--cur[e] == 0) que.pb(e); } } each(v, vis) lvl[v] = -1; que.clear(); vis.clear(); } rep(i, 1, n) ans[i] += ans[i-1]; each(a, ans) cout << a << ' '; cout << '\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 | #define _USE_MATH_DEFINES #include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using Vi = vector<int>; using Pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(12); int n; cin >> n; vector<Vi> inv(n); Vi deg(n); rep(i, 0, n) { int k; cin >> k; while (k--) { int j; cin >> j; j--; inv[j].pb(i); deg[i]++; } } vector<ll> ans(n, -1); Vi vis, que, lvl(n, -1), cur = deg; que.reserve(n+5); vis.reserve(n+5); rep(b, 0, n) { lvl[b] = b; cur[b] = 0; que.pb(b); vis.pb(b); rep(pos, 0, sz(que)) { int v = que[pos]; ans[lvl[v]]++; each(e, inv[v]) { if (lvl[e] == -1) { lvl[e] = e; cur[e] = deg[e]; vis.pb(e); } lvl[e] = max(lvl[e], lvl[v]); if (--cur[e] == 0) que.pb(e); } } each(v, vis) lvl[v] = -1; que.clear(); vis.clear(); } rep(i, 1, n) ans[i] += ans[i-1]; each(a, ans) cout << a << ' '; cout << '\n'; return 0; } |