#include<bits/stdc++.h> #define pb push_back using namespace std; typedef long long lld; constexpr int MAX_N = 200000; constexpr int INF = (1<<29) - 1; int err; int n; lld wyn; int taken[MAX_N+9]; vector<size_t> v[MAX_N+9], rev[MAX_N+9], pth[MAX_N+9], que[MAX_N+9]; void psh(int i, int j) { //printf("BEGIN: psh(%d, %d)\n", i, j); if(pth[i][j] == v[i].size()) { //printf("UP: psh(%d, %d)\n", i, j); if(i != j) ++wyn; for(size_t h = 0; h < rev[i].size(); ++h) { //printf("adding pth[%u][%u]\n", rev[i][h], j); ++pth[rev[i][h]][j]; if(taken[rev[i][h]]) { psh(rev[i][h], j); } } } } int main() { err = scanf("%d", &n); for(int i = 1; i <= n; ++i) { int mx = i; int m; scanf("%d", &m); for(int j = 0; j < m; ++j) { int a; scanf("%d", &a); v[i].push_back(a); rev[a].push_back(i); mx = max(mx, a); } for(int j = 0; j <= n; ++j) { pth[i].push_back(0); } que[mx].push_back(i); } for(int ii = 1; ii <= n; ++ii) { //while(que[ii].size()) { //int i = que[ii].back(); //que[ii].pop_back(); int i = ii; //printf("TAKING %d\n", i); taken[i] = 1; pth[i][i] = v[i].size(); for(int j = 1; j <= ii; ++j) { if(pth[i][j] == v[i].size()) { psh(i,j); /* for(size_t h = 0; h < rev[i].size(); ++h) { ++pth[rev[i][h]][j]; if(taken[v[i][h]]) { psh(v[i][h], j); } } //*/ } } //} printf("%lld ", wyn); } } /* 9 2 2 3 1 1 1 2 1 5 3 5 8 9 1 5 2 6 4 2 5 9 3 5 8 5 //*/
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 | #include<bits/stdc++.h> #define pb push_back using namespace std; typedef long long lld; constexpr int MAX_N = 200000; constexpr int INF = (1<<29) - 1; int err; int n; lld wyn; int taken[MAX_N+9]; vector<size_t> v[MAX_N+9], rev[MAX_N+9], pth[MAX_N+9], que[MAX_N+9]; void psh(int i, int j) { //printf("BEGIN: psh(%d, %d)\n", i, j); if(pth[i][j] == v[i].size()) { //printf("UP: psh(%d, %d)\n", i, j); if(i != j) ++wyn; for(size_t h = 0; h < rev[i].size(); ++h) { //printf("adding pth[%u][%u]\n", rev[i][h], j); ++pth[rev[i][h]][j]; if(taken[rev[i][h]]) { psh(rev[i][h], j); } } } } int main() { err = scanf("%d", &n); for(int i = 1; i <= n; ++i) { int mx = i; int m; scanf("%d", &m); for(int j = 0; j < m; ++j) { int a; scanf("%d", &a); v[i].push_back(a); rev[a].push_back(i); mx = max(mx, a); } for(int j = 0; j <= n; ++j) { pth[i].push_back(0); } que[mx].push_back(i); } for(int ii = 1; ii <= n; ++ii) { //while(que[ii].size()) { //int i = que[ii].back(); //que[ii].pop_back(); int i = ii; //printf("TAKING %d\n", i); taken[i] = 1; pth[i][i] = v[i].size(); for(int j = 1; j <= ii; ++j) { if(pth[i][j] == v[i].size()) { psh(i,j); /* for(size_t h = 0; h < rev[i].size(); ++h) { ++pth[rev[i][h]][j]; if(taken[v[i][h]]) { psh(v[i][h], j); } } //*/ } } //} printf("%lld ", wyn); } } /* 9 2 2 3 1 1 1 2 1 5 3 5 8 9 1 5 2 6 4 2 5 9 3 5 8 5 //*/ |