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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define mp make_pair
#define all(a) begin(a),end(a)
#define FOR(x,val,to) for(int x=(val);x<int((to));++x)
#define FORE(x,val,to) for(auto x=(val);x<=(to);++x)
#define FORR(x,arr) for(auto &x: arr)
#define FORS(x,plus,arr) for(auto x = begin(arr)+(plus); x != end(arr); ++x)
#define FORREV(x,plus,arr) for(auto x = (arr).rbegin()+(plus); x !=(arr).rend(); ++x)
#define REE(s_) {cout<<s_<<'\n';exit(0);}
#define GET(arr) for(auto &i: (arr)) sc(i)
#define whatis(x) cerr << #x << " is " << (x) << endl;
#define e1 first
#define e2 second
#define INF 0x7f7f7f7f
typedef std::pair<int,int> pi;
typedef std::vector<int> vi;
typedef std::vector<std::string> vs;
typedef int64_t ll;
typedef uint64_t ull;
#define umap unordered_map
#define uset unordered_set
using namespace std;
using namespace __gnu_pbds;

#ifdef ONLINE_JUDGE
#define whatis(x) ;
#endif
#ifdef _WIN32
#define getchar_unlocked() _getchar_nolock()
#define _CRT_DISABLE_PERFCRIT_LOCKS
#endif
template<class L, class R> ostream& operator<<(ostream &os, map<L, R> P) { for(auto const &vv: P)os<<"("<<vv.first<<","<<vv.second<<")"; return os; }
template<class T> ostream& operator<<(ostream &os, set<T> V) { os<<"[";for(auto const &vv:V)os<<vv<<","; os<<"]"; return os; }
template<class T> ostream& operator<<(ostream &os, vector<T> V) { os<<"[";for(auto const &vv:V)os<<vv<<","; os<<"]"; return os; }
template<class L, class R> ostream& operator<<(ostream &os, pair<L, R> P) { os<<"("<<P.first<<","<<P.second<<")"; return os; }
inline int fstoi(const string &str){auto it=str.begin();bool neg=0;int num=0;if(*it=='-')neg=1;else num=*it-'0';++it;while(it<str.end()) num=num*10+(*it++-'0');if(neg)num*=-1;return num;}
inline void getch(char &x){while(x = getchar_unlocked(), x < 33){;}}
inline void getstr(string &str){str.clear(); char cur;while(cur=getchar_unlocked(),cur<33){;}while(cur>32){str+=cur;cur=getchar_unlocked();}}
template<typename T> inline bool sc(T &num){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){if(c == EOF) return false;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; return true;}template<typename T, typename ...Args> inline void sc(T &num, Args &...args){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; sc(args...); }
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //s.find_by_order(), s.order_of_key() <- works like lower_bound
template<typename T> using ordered_map = tree<T, int, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define N 1000001

/* #define whatis(x) ; */

int n;
int d[N];
ll res;
/* constexpr int M = 20; */
/* int p[N][M]; */
int nsuccdeg[N];
int nbckn[N];
/* vector<int> succque; */
// nice comparator btw xddd
/* priority_queue<int, vector<int>, less<int>> succque; */
priority_queue<int, vector<int>, greater<int>> succque;
bool isbackn[N];
bool issuckn[N];
vi adj[N];
vi adjrev[N];
vi nwadjrev[N];
int nxt[N];
bool isoncyc[N];
vi adjcomp[N]; // pop if 2 te same, lazy, break if >= 2 comps.

int sz[N];
int link[N];

inline int find(int a){
    return a == link[a] ? a : link[a] = find(link[a]);
}

inline bool unite(int a, int b){ //a and b are after find() //or not, whatever
    a = find(a);
    b = find(b);
    if(a == b) return false;
    if(sz[a] < sz[b]) swap(a,b);
    sz[a] += sz[b];
    link[b] = a;
    return true;
}

void makebck(int v){
    whatis("makebck")
    whatis(v)
    whatis(adjrev[v])
    // adjrev[v] is [0,1,2,3,5,6,7,8,]?
    assert(!isbackn[v]);
    isbackn[v] = 1;
    FORR(i,adjrev[v]){
        --nsuccdeg[i];
        /* whatis(i) */
        /* whatis(nsuccdeg[i]) */
        /* whatis(nbckn[i]) */
        /* if(nsuccdeg[i] == 0 && nbckn[i] <= 1) */
        /*     succque.push(i); */
        if(adjcomp[i].size() == 1){
            succque.push(i);
        }
        if(adjcomp[i].size() > 1){
            while(adjcomp[i].size() >= 2){
                int v1 = adjcomp[i][adjcomp[i].size() - 2];
                int v2 = adjcomp[i][adjcomp[i].size() - 1];
                // mógłbym lca niby próbować, ale might just as well dsu.
                if(find(v1) == find(v2)){
                    adjcomp[i].pop_back();
                }
                else{
                    break;
                }
            }
            if(adjcomp[i].size() == 1){
                succque.push(i);
            }
        }
    }
}

vi vec;
int tab[N];

// guess uwaga na cykl.
void propd(int v, int nwd){
    whatis(v)
    whatis(nwadjrev[v])
    res -= d[v];
    d[v] = nwd;
    res += d[v];
    FORR(i,nwadjrev[v]){
        if(isoncyc[i])
            continue;
        /* propd(v, nwd + 1); */
        propd(i, nwd + 1);
    }
}

void makesucc(int v){
    whatis("makesucc")
    whatis(v)
    assert(isbackn[v]);
    assert(!issuckn[v]);
    issuckn[v] = 1;
    whatis(tab[v])
    int wh = -1;
    if(!adj[v].empty()){
        FOR(i,1,adj[v].size()){
            int cr = adj[v][i];
            while(cr != -1){
                if(++tab[cr] == 1)
                    vec.push_back(cr);
                cr = nxt[cr];
            }
        }
        {
            int cr = adj[v][0];
            whatis(adj[v][0])
            // zapętlanie tu hmmm.
            while(cr != -1){
                whatis(cr)
                whatis(nxt[cr])
                /* if(tab[cr] == adj[v].size() - 1){ */
                // bruh.
                /* if(++tab[cr] == adj[v].size()){ */
                if(++tab[cr] == 1){
                    vec.push_back(cr);
                }
                if(tab[cr] == adj[v].size()){
                    /* wh = cr; */
                    /* break; */
                    if(wh == -1)
                        wh = cr;
                }
                cr = nxt[cr];
            }
        }
    }
    whatis(tab[v])
    if(wh == -1){
        whatis("wh == -1")
        // nothing basically either i guess.
    }
    else if(wh == v){
        whatis("wh == v")
        // nothing basically i guess.
    }
    // jak adj.size() == 1, to może tu być zero.
    // -> let's chg algos to inc always dla bezpiezceństwa then.
    // czy nie powinno być tab[v] == adj[v].size()?
    else if(tab[v]){
        assert(tab[v] == adj[v].size());
        whatis("tab[v]")
        /* nxt[v] = wh; */ // anti-zapętlanie i guess.
        /* nxt[v] = wh; */
        int cycsz = 1;
        int cr = wh;
        isoncyc[v] = 1;
        while(cr != v){
            ++cycsz;
            // out of bounds, kox.
            isoncyc[cr] = 1;
            cr = nxt[cr];
        }
        whatis(cycsz)
        cr = wh;
        /* res -= d[v]; */
        /* d[v] = cycsz; */
        /* d[v] = cycsz - 1; */
        /* res += d[v]; */
        propd(v, cycsz - 1);
        /* propd(v, cycsz); */
        /* cr = nxt[v]; */ // that's why dif.
        cr = wh;
        while(cr != v){
            propd(cr, cycsz - 1);
            /* propd(cr, cycsz); */
            cr = nxt[cr];
        }
    }
    else{
        whatis("else")
        nxt[v] = wh;
        nwadjrev[wh].push_back(v);
        whatis(d[wh])
        propd(v, d[wh] + 1);
        unite(v, wh);
    }
    FORR(i,vec)
        tab[i] = 0;
    vec.clear();
    FORR(i,adjrev[v]){
        if(isbackn[v]){
            --nbckn[i];
            /* if(nsuccdeg[i] == 0 && nbckn[i] == 1) */
            /*     succque.push(i); */
        }
        else{
            --nbckn[i];
            --nsuccdeg[i];
            /* if(nsuccdeg[i] == 0 && nbckn[i] <= 1) */
            /*     succque.push(i); */
        }
        if(adjcomp[i].size() > 1){
            while(adjcomp[i].size() >= 2){
                int v1 = adjcomp[i][adjcomp[i].size() - 2];
                int v2 = adjcomp[i][adjcomp[i].size() - 1];
                // mógłbym lca niby próbować, ale might just as well dsu.
                if(find(v1) == find(v2)){
                    adjcomp[i].pop_back();
                }
                else{
                    break;
                }
            }
            // kox miejsce na wywalenie ifa
            if(adjcomp[i].size() == 1){
                succque.push(i);
            }
        }
    }
    isbackn[v] = 1;
}

int main(){
    for(int i = 0; i < N; ++i){
        link[i] = i;
        sz[i] = 1;
    }
    ios_base::sync_with_stdio(0);cin.tie(0);
    memset(nxt,-1,sizeof nxt);
    sc(n);
    FOR(i,0,n){
        int m;
        sc(m);
        bool hasself = 0;
        FOR(x,0,m){
            int cr;
            sc(cr);
            --cr;
            // can't ignore that xd.
            if(cr == i){
                hasself |= 1;
            }
                /* continue; */
            // bruh.
            /* adj[i].push_back(x); */
            // to na kox wejściu testowałem init wersję btw xd.
            adj[i].push_back(cr);
        }
        if(hasself)
            adj[i].clear();
        sort(all(adj[i]));
        adj[i].erase(unique(all(adj[i])), adj[i].end());
        /* whatis(i) */
        /* whatis(adj[i]) */
        FORR(x,adj[i])
            adjrev[x].push_back(i);
        // no casing at this point.
        nsuccdeg[i] = adj[i].size();
        nbckn[i] = adj[i].size();
        /* if(!adj[i].empty() && !nsuccdeg[i]){ */
        /* /1* if(adj[i].){ *1/ */
        /*     succque.push(i); */
        /* } */
        adjcomp[i] = adj[i];
        /* if(adj[i].size() == 1){ // == 0 po co i guess. */
        /*     succque.push(i); */
        /* } */
    }
    FOR(i,0,n){
        whatis(i)
        /* if(!nsuccdeg[i]) */
        /* if(!adj[i].empty() && !nsuccdeg[i]){ */
        /*     succque.push(i); */
        /* } */
        /* if(adj[i].size() == 0){ */
        /*     makebck(i); */
        /* } */
        makebck(i);
        /* if(adj[i].size() == 1){ */
        /*     // w sumie, może być, jesli jest 0 out-deg nodem o mniejszym id. */
        /*     /1* assert(!isbackn[adj[i][0]]); *1/ */
        /*     // assert !issucc? */
        /*     // todo co jak backn ma większe id? */
        /*     // do i defer it? */
        /*     // bo wciąż valid jest jak jest succ bez adj backna i guess. */
        /*     // btw, wait, can't do that, no bo id może być większe. */
        /*     // -> let's implictely call it at beg for everything. */
        /*     // może warunek z tym, że max 1 to, jest tym co gwarantuje */
        /*     // poprawność. */
        /*     /1* if(!isbackn[adj[i][0]]) *1/ */
        /*     /1*     makebck(adj[i][0]); *1/ */
        /*     // redundant, bo w makebck na pewno będzie zpushowany. */
        /*     /1* succque.push(i); *1/ */
        /* } */
        /* whatis(succque.size()) */
        if(succque.size())
            whatis(succque.top())
        while(!succque.empty() && succque.top() <= i){
            int v = succque.top(); succque.pop();
            makesucc(v);
        }
        cout << res << ' ';
    }
    FOR(i,0,n){
        whatis(d[i])
        whatis(issuckn[i])
        whatis(adjcomp[i].size())
    }
    cout << '\n';
}