#pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> ii; typedef vector<int> vi; typedef double K; constexpr int INF = 0x3f3f3f3f; #define FOR(i, b, e) for(int i = (b); i < (e); i++) #define TRAV(x, a) for(auto &x: (a)) #define SZ(x) ((int)(x).size()) #define PB push_back #define X first #define Y second constexpr int N = 2e5 + 5; constexpr int KK = 10'003; int n; vi G[N]; int ans[N], vis[N], ozn[N], ito; int arr[KK][KK]; bool path[N], checked[N], checking[N]; int iter, maks; bool dfs(int v, int block) { vis[v] = iter; path[v] = true; maks = max(maks, v); TRAV(x, G[v]) { if(x == block) continue; if(vis[x] < iter && dfs(x, block)) break; if(path[x]) { maks = N - 1; break; } } path[v] = false; return maks == N - 1; } void licz(int st, int block) { maks = block; iter++; dfs(st, block); arr[st][block] = maks + 1; ans[maks]++; } void check(int st) { if(checked[st]) return; checking[st] = true; int son = G[st][0]; if(SZ(G[st]) == 1 && !checking[son]) { check(son); arr[st][son] = max(st, son) + 1; ans[max(st, son)]++; FOR(i, 0, n) if(arr[son][i] && i != st) { int co = max(st, arr[son][i] - 1); arr[st][i] = co + 1; ans[co]++; } } else { ito++; int v = st; ozn[v] = ito; while(ozn[G[v][0]] < ito) { v = G[v][0]; ozn[v] = ito; licz(st, v); } } checked[st] = true; } void solve() { cin >> n; FOR(i, 0, n) { int a; cin >> a; G[i].resize(a); TRAV(x, G[i]) { cin >> x; x--; } } mt19937 g(325818517); FOR(i, 0, n) shuffle(G[i].begin(), G[i].end(), g); FOR(i, 0, n) check(i); FOR(i, 1, n) ans[i] += ans[i - 1]; FOR(i, 0, n) cout << ans[i] << " \n"[i + 1 == n]; } int main() { ios::sync_with_stdio(0); cin.tie(0); // int tt; cin >> tt; // FOR(te, 0, tt) { // // cout << "Case #" << te + 1 << ": "; // solve(); // } 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | #pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> ii; typedef vector<int> vi; typedef double K; constexpr int INF = 0x3f3f3f3f; #define FOR(i, b, e) for(int i = (b); i < (e); i++) #define TRAV(x, a) for(auto &x: (a)) #define SZ(x) ((int)(x).size()) #define PB push_back #define X first #define Y second constexpr int N = 2e5 + 5; constexpr int KK = 10'003; int n; vi G[N]; int ans[N], vis[N], ozn[N], ito; int arr[KK][KK]; bool path[N], checked[N], checking[N]; int iter, maks; bool dfs(int v, int block) { vis[v] = iter; path[v] = true; maks = max(maks, v); TRAV(x, G[v]) { if(x == block) continue; if(vis[x] < iter && dfs(x, block)) break; if(path[x]) { maks = N - 1; break; } } path[v] = false; return maks == N - 1; } void licz(int st, int block) { maks = block; iter++; dfs(st, block); arr[st][block] = maks + 1; ans[maks]++; } void check(int st) { if(checked[st]) return; checking[st] = true; int son = G[st][0]; if(SZ(G[st]) == 1 && !checking[son]) { check(son); arr[st][son] = max(st, son) + 1; ans[max(st, son)]++; FOR(i, 0, n) if(arr[son][i] && i != st) { int co = max(st, arr[son][i] - 1); arr[st][i] = co + 1; ans[co]++; } } else { ito++; int v = st; ozn[v] = ito; while(ozn[G[v][0]] < ito) { v = G[v][0]; ozn[v] = ito; licz(st, v); } } checked[st] = true; } void solve() { cin >> n; FOR(i, 0, n) { int a; cin >> a; G[i].resize(a); TRAV(x, G[i]) { cin >> x; x--; } } mt19937 g(325818517); FOR(i, 0, n) shuffle(G[i].begin(), G[i].end(), g); FOR(i, 0, n) check(i); FOR(i, 1, n) ans[i] += ans[i - 1]; FOR(i, 0, n) cout << ans[i] << " \n"[i + 1 == n]; } int main() { ios::sync_with_stdio(0); cin.tie(0); // int tt; cin >> tt; // FOR(te, 0, tt) { // // cout << "Case #" << te + 1 << ": "; // solve(); // } solve(); return 0; } |