#include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto & i : (a)) #define SZ(x) ((int)(x).size()) #define X first #define Y second #define PR std::pair #define MP std::make_pair typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; int n; std::vector<VI> g; constexpr int N = 200'000; ll ans[N]; int indeg_orig[N]; int indeg[N]; void jazda(int start){ std::copy(indeg_orig, indeg_orig + n, indeg); std::priority_queue<int, std::vector<int>, std::greater<int>> que; que.push(start); int have = 0; int cur = 0; while(!que.empty()){ int v = que.top(); que.pop(); while(cur < v) ans[cur++] += have; have++; TRAV(ch, g[v]) if(--indeg[ch] == 0 && ch != start) que.push(ch); } while(cur < n) ans[cur++] += have; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cin >> n; g = std::vector<VI>(n); FOR(i, n){ int l; std::cin >> l; FOR(j, l){ int a; std::cin >> a; a--; g[a].push_back(i); indeg_orig[i]++; } } FOR(i, n) jazda(i); FOR(i, n) std::cout << ans[i] - i - 1 << " "; std::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 | #include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto & i : (a)) #define SZ(x) ((int)(x).size()) #define X first #define Y second #define PR std::pair #define MP std::make_pair typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; int n; std::vector<VI> g; constexpr int N = 200'000; ll ans[N]; int indeg_orig[N]; int indeg[N]; void jazda(int start){ std::copy(indeg_orig, indeg_orig + n, indeg); std::priority_queue<int, std::vector<int>, std::greater<int>> que; que.push(start); int have = 0; int cur = 0; while(!que.empty()){ int v = que.top(); que.pop(); while(cur < v) ans[cur++] += have; have++; TRAV(ch, g[v]) if(--indeg[ch] == 0 && ch != start) que.push(ch); } while(cur < n) ans[cur++] += have; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cin >> n; g = std::vector<VI>(n); FOR(i, n){ int l; std::cin >> l; FOR(j, l){ int a; std::cin >> a; a--; g[a].push_back(i); indeg_orig[i]++; } } FOR(i, n) jazda(i); FOR(i, n) std::cout << ans[i] - i - 1 << " "; std::cout << "\n"; return 0; } |