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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pi pair<int, int>
#define vi vector<int>
#define mod 1000000007
template<typename T> bool chkmin(T &a, T b){return (b < a) ? a = b, 1 : 0;}
template<typename T> bool chkmax(T &a, T b){return (b > a) ? a = b, 1 : 0;}
ll ksm(ll a, ll b) {if (b == 0) return 1; ll ns = ksm(a, b >> 1); ns = ns * ns % mod; if (b & 1) ns = ns * a % mod; return ns;}
using namespace std;
const int maxn = 200005;
int n;
int fa[maxn];
int sz[maxn], dep[maxn];
vi items[maxn]; // item in each 
int nx[maxn][20];
void initdsu() {
    for (int i = 0; i < maxn; i++)
        fa[i] = i, sz[i] = 1, dep[i] = 0, items[i] = vi{i};
}
int gfa(int a) {
    if (fa[a] == a) return a;
    int nf = fa[a];
    int res = gfa(nf);
    fa[a] = res, dep[a] += dep[nf];
    return res;
}
int gdep(int a) {
    gfa(a);
    return dep[a];
}
ll ans = 0;
vi teg[maxn];
int fl[maxn];
void dfs(int a) {
    for (auto v : teg[a])
        dep[v] = dep[a] + 1, dfs(v);
}
int cyc[maxn];

int findf(int a, int b) {
    if (nx[a][b] != -1) return nx[a][b];
    if ((1 << b) > gdep(a)) return -1;
    int u = findf(a, b - 1);
    int ans = findf(u, b - 1);
    return nx[a][b] = ans;
}
void reb(int a) {
    // rebuild the block 
    for (auto v : items[a])
        ans -= gdep(v);
    vi cur;
    int p = a;
    while (1) {
        cur.pb(p);
        p = nx[p][0];
        if (p == a) break;
    }
    for (auto v : cur) fl[v] = 1;
    int k = cur.size();
    dep[a] = 0;
    for (auto v : items[a]) {
        dep[v] = 0;
        if (fl[v]) continue;
        int e = nx[v][0];
        if (fl[e]) e = a;
        teg[e].pb(v);
    }
    dfs(a);
    for (auto v : items[a]) 
        ans += dep[v] + (k - 1);
    for (auto v : items[a])
        fa[v] = a;

    for (int j = 0; j < 20; j++)
        for (auto v : items[a]) {
            if (j == 0) {
                if (fl[nx[v][0]]) nx[v][0] = a;
            }
            else nx[v][j] = -1, findf(v, j);
        }
    cyc[a] = k;
}

int lca(int a, int b) {
    if (gdep(a) < gdep(b)) swap(a, b);
    for (int i = 19; i >= 0; i--) 
        if (gdep(a) - gdep(b) >= (1 << i))
            a = findf(a, i);
    for (int i = 19; i >= 0; i--) 
        if (findf(a, i) != findf(b, i)) 
            a = findf(a, i), b = findf(b, i);
        
    if (a != b) a = findf(a, 0);
    return a;
}
pi q[maxn];
int fr, ed;
void init() {
    initdsu();
    memset(nx, -1, sizeof(nx));
}
int k;
vi eg[maxn], bk[maxn];

int chkd[maxn];
void chk(int a) {
    if (a > k) return ;
    if (chkd[a]) return ;
    int ns = 0;
    for (auto v : eg[a]) {
        if (v > k) return ;
        else {
            int nf = gfa(v);
            if (ns == 0) ns = nf;
            else if (ns != nf) return ;
        }
    }
    int cr = eg[a][0];
    for (auto v : eg[a]) 
        cr = lca(cr, v);
    q[ed++] = mp(a, cr);
    chkd[a] = 1;
}
void lk(int a, int b) {
    // fa[a] = b
    nx[a][0] = b;
    assert(fa[a] == a);
    if (gfa(a) == gfa(b)) {
        reb(a);
        return ;
    }
    else {
        int bf = gfa(b), bdep = gdep(b);
        fa[a] = bf, dep[a] = bdep + 1;
        ans += 1ll * (bdep + 1 + max(0, cyc[bf] - 1)) * sz[a];
        sz[bf] += sz[a];
        if (items[bf].size() < items[a].size()) 
            swap(items[bf], items[a]);
        items[bf].insert(items[bf].end(), items[a].begin(), items[a].end());
        for (auto h : items[a])
            for (auto g : bk[h])
                chk(g);
    }
}
void addall() {
    while (fr < ed) {
        lk(q[fr].fi, q[fr].se);
        fr += 1;
    }
}
int main() {  
    cin >> n;
    init();
    for (int i = 1; i <= n; i++) {
        int s; scanf("%d", &s);
        eg[i].resize(s);
        for (int j = 0; j < s; j++)
            scanf("%d", &eg[i][j]);
        for (auto v : eg[i])
            bk[v].pb(i);
    }
    for (int i = 1; i <= n; i++) {
        //cout << "WORK" << i << endl;
        k = i;
        for (auto v : bk[i]) 
            if (v <= i)
                chk(v);
        chk(i);
        addall();
        printf("%lld ", ans);
    }
    cout << endl;
    return (0-0); // <3 cxr
}
/*
20
2 19 3
1 5 
1 6 
1 4 
1 3 
1 2 
2 2 10
1 13 
1 3 
1 14 
1 16 
1 8 
1 5 
1 5 
1 18 
1 2 
1 4 
1 11 
1 17 
1 13 
*/