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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#include <bits/stdc++.h>
#include <limits>

using namespace std;

typedef unsigned long long int ulli;
typedef long long int lli;

const lli base=100000000;

const int bnlen=20;

struct bignum {
    lli nums[bnlen];
    bignum() {
       this->nums[0]=1;
       for(int i=1; i<bnlen; ++i) this->nums[i]=0;
    }

    bignum( lli init ) {
        this->nums[0]=init;
        for(int i=1; i<bnlen; ++i) this->nums[i]=0;
    }

    void add(bignum bn) {
        int carry=0;
        for(int i=0; i<bnlen; ++i) {
            this->nums[i]+=bn.nums[i];
            this->nums[i]+=carry;

            carry=this->nums[i]/base;
            this->nums[i] %= base;
        }
    }

    void subs(bignum bn) {
        int carry=0;
        for(int i=0; i<bnlen; ++i) {
            this->nums[i]= this->nums[i]-bn.nums[i]+carry;
            carry=0;
            if(this->nums[i] < 0)
            {
                carry=-1;
                this->nums[i]+=base;

            }
        }
    }

    void multsm(lli small) {
        lli carry=0;
        for(int i=0; i<bnlen; ++i) {
            this->nums[i]*=small;
            this->nums[i]+=carry;
            carry=this->nums[i]/base;
            this->nums[i] %= base;
        }
    }

    void shift(int i) {
        if(i>0) {
            int j;
            for(j=bnlen-1; j>=i; --j) {
                this->nums[j]=this->nums[j-i];
            }
            while(j >= 0) { this->nums[j]=0; --j; }
        }
    }

    void ccpy(bignum bn) {
        for(int i=0; i<bnlen; ++i) this->nums[i]=bn.nums[i];
    }

    void mult(bignum bn) {
        bignum sum(0);
        bignum temporal(0);

        temporal.ccpy(*this);
        for(int i=0; i<bnlen; ++i) {
            temporal.multsm(bn.nums[i]);
            temporal.shift(i);
            sum.add(temporal);
            temporal.ccpy(*this);

        }
        this->ccpy(sum);
    }


    void div2() {
        int carry=0;
        for(int i=bnlen-1; i>=0; --i) {
            if(carry) this->nums[i]+=base;
            carry=this->nums[i]%2; // tu wstawic shifty bitowe
            this->nums[i] /=2;  // tu tez

        }
    }

    bool imless(bignum bn) {

        for(int i=bnlen-1; i>=0; --i) {
            if(this->nums[i] < bn.nums[i]) return true;
            if(this->nums[i] > bn.nums[i]) return false;
        }
        return false;
    }

    bool less(bignum bn1, bignum bn2) {
        for(int i=bnlen-1; i>=0; --i) {
            if(bn1.nums[i] < bn2.nums[i]) return true;
            if(bn1.nums[i] > bn2.nums[i]) return false;
        }
        return false;
    }

    // czy duza liczba jest rowna malej
    bool equalssm(lli a) {
        if(this->nums[0]==a && this->nums[1]==0 &&
                        this->nums[0]==0 && this->nums[1]==0 &&
                        this->nums[0]==0 && this->nums[1]==0)
            return true;
        return false;
    }

    void divide(bignum bn) {
        if( bn.equalssm(1) ) return;
        if( bn.equalssm(2) ) {
            this->div2();
            return;
        }

        bignum last(1);
        bignum cp(1); cp.ccpy(bn); cp.mult(last);
        while(less(cp,*this)) {
            last.multsm(2);
            cp.multsm(2);
        }

        bignum first(1); bignum firstcp(1);

        while(less(firstcp,last)) {
            bignum middle(0); middle.add(first); middle.add(last); middle.div2();
            cp.ccpy(bn);
            cp.mult(middle);
            if(less(cp,*this)) first.ccpy(middle);
            else last.ccpy(middle);
            firstcp.ccpy(first);
            firstcp.add(1);
        }

        this->ccpy(last);

    }

    //przy wypisywaniu zakladamy ze duze liczby sa >=1
    void print() {
        bool zeros=true;
        for(int i=bnlen-1; i>=0; --i) {
                if(zeros && this->nums[i] > 0) {
                    zeros=false;
                    printf("%lld",this->nums[i]);
                }
                else {
                    if(!zeros) {

                       for(lli j=base/10; j>=1; j/=10) {
                          if(nums[i] >= j) {
                              printf("%lld",this->nums[i]);
                              break;
                          }
                          else printf("0");
                       }

                    }

                }

        }
        printf("\n");
    }
};

bignum gcd(bignum a, bignum b)
{
    /* GCD(0, b) == b; GCD(a, 0) == a,
       GCD(0, 0) == 0 */
    if (a.equalssm(0))
        return b;
    if (b.equalssm(0))
        return a;

   // printf("in gcd \n");

    /*Finding K, where K is the
      greatest power of 2
      that divides both a and b. */
    int k;
    for (k = 0; ((a.nums[0] | b.nums[0]) & 1) == 0; ++k)
    {
        a.div2();
        b.div2();
    }

   // printf("k=%d\n",k);
   // a.print();
   // b.print();

    /* Dividing a by 2 until a becomes odd */
    while ((a.nums[0] & 1) == 0)
        a.div2();

    //printf("a reduced: \n");
    //a.print();
    /* From here on, 'a' is always odd. */
    //int jjj=2;
    do
    {
      //  --jjj;
        /* If b is even, remove all factor of 2 in b */
        while ((b.nums[0] & 1) == 0)
            b.div2();

     //   printf("b reduced: \n");
     //   printf("b="); b.print();

        /* Now a and b are both odd.
           Swap if necessary so a <= b,
           then set b = b - a (which is even).*/
        if (b.imless(a)) {
     //       printf("I AM LESS!\n");
            swap(a, b); // Swap u and v.
     //       a.print();
     //       b.print();
          }
        b.subs(a);
     //   printf("after substraction: \n");
     //   printf("a="); a.print();
     //   printf("b="); b.print();
    }while(! b.equalssm(0));

   // printf("after the loop\n");

    /* restore common factors of 2 */
    for(int j=0; j<k; ++j) a.mult(2);  // shift bitowy ??
    return a;
}

bignum lcm(bignum a,bignum b) {
    a.divide(gcd(a,b));
    a.mult(b);
    return a;
}

struct edge {
    bignum num;
    bignum den;

    edge() {
        num=bignum(1);
        den=bignum(1);
    }

    edge(ulli i)  {
        num=bignum(i);
        den=bignum(1);
    }

    void reduce() {
        bignum d(1);
//        printf("inside reduction\n");
        d.ccpy(gcd(this->num,this->den));
//        printf("gcd =");
//        d.print();

        (this->num).divide(d);
        (this->den).divide(d);
    }

    void add(edge e) {
        bignum dencp(1);
        dencp.ccpy(this->den);
        dencp.mult(e.num);
//        printf("after 1\n");
        (this->num).mult(e.den);
//        printf("after 2\n");
        (this->num).add(dencp);
//        printf("after 3\n");
        (this->den).mult(e.den);
//        printf("after 4\n");
//        printf("before reduction: \n");
//        this->print();
        this->reduce();
    }

    void divide(lli i) {
        assert(i>0);
        bignum d(1);
        bignum ii(i);

//        printf("inside division, dividing :");
 //       this->print();
//        printf("by: ");
//        ii.print();

        d.ccpy(gcd(ii,this->num));

//        printf("gcd: "); d.print();
        (this->num).divide(d);
//        printf("numerator: ");
//        this->num.print();
        ii.divide(d);
//        printf("by (after dividing by gcd) :");
//        ii.print();
        (this->den).mult(ii);

//        printf("after division: "); this->print();
    }

    void cpy(edge e) {
        this->num.ccpy(e.num);
        this->den.ccpy(e.den);
    }

    void print() {
        (this->num).print();
        (this->den).print();
    }
};

vector<edge> edges;

struct node {
    vector<int> inedges;
    vector<int> outedges;
    bool exists;
    node() : exists(false) {}

};

vector<node> nodes;

void print_graph(int n) {
    for(int i=0; i<n; ++i) {
        printf("%d:\n", i);
        printf("out:");
        for(auto x : nodes[i].outedges) printf("%d ", x); printf("\n");
        printf("in:");
        for(auto x : nodes[i].inedges) printf("%d ", x); printf("\n");
    }
}

void propagate_edges(int n) {
    for(int i=1; i<n; ++i) {
        if(nodes[i].exists && (nodes[i].outedges).size()>0 ) {
//           printf("node %d propagates :\n",i);
           edge e(0);
//           printf("prefix sums of inedges:\n");
           for(auto j: nodes[i].inedges) {
//               printf("+ "); edges[j].print();
               e.add(edges[j]);
//               printf(" = ");
//               e.print();
           }
//           printf("inedge sum: \n"); e.print();
           lli outdeg=(nodes[i].outedges).size();
           e.divide(outdeg);
//           printf("after division by outdeg: \n"); e.print();
           for(auto j: nodes[i].outedges) edges[j].cpy(e);
        }
    }
}

int main() {
 //  bignum biggy(71738150);
 //  biggy.nums[1]=22;

 //  biggy.print();

 //  bignum biggy2(11240000);
 //  biggy2.nums[1]=55;

 //  biggy2.print();

 //  bignum gcdd(1);
 //  gcdd.ccpy(gcd(biggy,biggy2));
 //  gcdd.print();

 //  biggy2.subs(biggy);
 //  biggy2.print();



   int n; int m=0;
   scanf("%d",&n);
   nodes.resize(n);
   for(int i=0; i<n; ++i) {

       if(i==0 || (nodes[i].inedges).size() > 0)
          nodes[i].exists=true;

          int r; int neighbor;
          scanf("%d",&r);  //printf("%d ",r);
          for(int j=0; j<r; ++j) {
              scanf("%d",&neighbor); //printf("%d ",neighbor-1);
              if(nodes[i].exists) {
                 (nodes[i].outedges).push_back(m);
                 (nodes[neighbor-1]).inedges.push_back(m);
                 ++m;
              }
          }
   }
   edges.resize(m);

//   print_graph(n);

//   printf("edges: %d\n",m);
//   for(int i=0; i<m; ++i) { printf("%d :",i); edges[i].print(); }

   propagate_edges(n);

//   printf("%d\n",m);
//   for(int i=0; i<m; ++i) { printf("%d :",i); edges[i].print(); }


   bignum res(1);
   for(int i=0; i<m; ++i) {

       res.ccpy(lcm(res,edges[i].den));
   }

   lli outdeg1=(nodes[0].outedges).size();

   if(outdeg1>0) res.multsm(outdeg1);
   res.print();

   return 0;
}