//Autor: Bartłomiej Czarkowski #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; int n, m, a; int blok[N]; int stp[N]; vector <int> v[N]; vector <int> r[N]; vector <int> poj[N]; int odp[N]; int wylicz() { int ret = 0; for (int i = 1; i <= n; ++i) { if (blok[i]) { continue; } for (int j = 1; j <= n; ++j) { stp[j] = v[j].size(); } stp[i] = 0; queue <int> que; que.push(i); --ret; while (!que.empty()) { int x = que.front(); que.pop(); ++ret; for (int j : r[x]) { if (blok[j]) { continue; } --stp[j]; if (!stp[j]) { que.push(j); } } } } return ret; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &m); for (int j = 1; j <= m; ++j) { scanf("%d", &a); v[i].push_back(a); r[a].push_back(i); } if (v[i].size() == 1) { poj[v[i][0]].push_back(i); } } for (int i = n; i; --i) { odp[i] = wylicz(); blok[i] = 1; } for (int i = 1; i <= n; ++i) { printf("%d ", odp[i]); } printf("\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 | //Autor: Bartłomiej Czarkowski #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; int n, m, a; int blok[N]; int stp[N]; vector <int> v[N]; vector <int> r[N]; vector <int> poj[N]; int odp[N]; int wylicz() { int ret = 0; for (int i = 1; i <= n; ++i) { if (blok[i]) { continue; } for (int j = 1; j <= n; ++j) { stp[j] = v[j].size(); } stp[i] = 0; queue <int> que; que.push(i); --ret; while (!que.empty()) { int x = que.front(); que.pop(); ++ret; for (int j : r[x]) { if (blok[j]) { continue; } --stp[j]; if (!stp[j]) { que.push(j); } } } } return ret; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &m); for (int j = 1; j <= m; ++j) { scanf("%d", &a); v[i].push_back(a); r[a].push_back(i); } if (v[i].size() == 1) { poj[v[i][0]].push_back(i); } } for (int i = n; i; --i) { odp[i] = wylicz(); blok[i] = 1; } for (int i = 1; i <= n; ++i) { printf("%d ", odp[i]); } printf("\n"); return 0; } |