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
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <chrono>
#include <functional>
#include <cstdlib>
#include <cassert>
using namespace std;

struct BigNumber
{
  static const int MAX_PARTS = 70;
  static const int BASE = 1000000;
  static const int BASE_LOG = 6;
  //static const int MAX_PARTS = 20;
  //static const int BASE = 10;
  //static const int BASE_LOG = 1;
  unsigned long long digits[MAX_PARTS];
  BigNumber()
  {
    for (int i=0; i<MAX_PARTS; ++i)
      digits[i] = 0;
  }
};

BigNumber fromLongLong(long long number)
{
  BigNumber result;
  int pos = 0;
  while (number > 0)
  {
    result.digits[pos] = (number % BigNumber::BASE);
    pos++;
    number = number / BigNumber::BASE;
  }
  return result;
}

std::string toString(BigNumber bigNumber)
{
  std::string result;
  int firstNonZero = false;
  int printAllDigits = false;
  for (int i= BigNumber::MAX_PARTS -1; i>=0; --i)
  {
    if (bigNumber.digits[i] > 0)
      firstNonZero = true;
    if (firstNonZero)
    {
      if (printAllDigits)
      {
        std::string str = std::to_string(bigNumber.digits[i]);
        str = std::string(BigNumber::BASE_LOG -str.size(), '0') + str;
        result += str;
      }
      else
        result+= std::to_string(bigNumber.digits[i]);
      printAllDigits = true;
    }
  }
  if (!firstNonZero)
    return "0";
  return result;
}

BigNumber operator +(BigNumber const& first, BigNumber const& second)
{
  unsigned long long sum = 0;
  BigNumber res;
  for (int i = 0; i < BigNumber::MAX_PARTS; ++i)
  {
    sum += first.digits[i] + second.digits[i];
    res.digits[i] = sum % BigNumber::BASE;
    sum /= BigNumber::BASE;
  }
  return res;
}

BigNumber operator -(BigNumber const& first, BigNumber const& second)
{
  BigNumber res;
  unsigned long long deficit = 0;
  for (int i = 0; i < BigNumber::MAX_PARTS; ++i)
  {
    if (first.digits[i] >= second.digits[i] + deficit)
    {
      res.digits[i] = first.digits[i] - second.digits[i] - deficit;
      deficit = 0;
    }
    else
    {
      res.digits[i] = first.digits[i] + BigNumber::BASE - second.digits[i] - deficit;
      deficit = 1;
    }
  }
  return res;
}

BigNumber operator *(BigNumber const& first, BigNumber const& second)
{
  unsigned long long sum = 0;
  BigNumber res;
  for (int i = 0; i < BigNumber::MAX_PARTS; ++i)
  {
    for (int j = 0; j <= i; ++j)
    {
        sum+= first.digits[j] * second.digits[i-j];
    }
    res.digits[i] = sum % BigNumber::BASE;
    sum /= BigNumber::BASE;
  }
  return res;
}

bool operator <(BigNumber const& first, BigNumber const& second)
{
  for (int i = BigNumber::MAX_PARTS - 1; i >= 0; --i)
  {
    if (first.digits[i] < second.digits[i])
      return true;
    if (first.digits[i] > second.digits[i])
      return false;
  }
  return false;
}

bool operator ==(BigNumber const& first, BigNumber const& second)
{
  for (int i = BigNumber::MAX_PARTS - 1; i >= 0; --i)
  {
    if (first.digits[i] != second.digits[i])
      return false;
  }
  return true;
}

BigNumber operator /(BigNumber const& first, BigNumber const& divisor)
{
  BigNumber result;
  BigNumber bigger = first;
  while (divisor < bigger || divisor == bigger)
  {
    if (bigger < fromLongLong(2) * divisor)
    {
      result = result + fromLongLong(1);
      return result;
    }
    int firstNonZeroBigger = -1;
    for (int i = BigNumber::MAX_PARTS - 1; i >= 0; --i)
    {
      if (bigger.digits[i] > 0)
      {
        firstNonZeroBigger = i;
        break;
      }
    }
    int firstNonZeroDivisor = -1;
    for (int i = BigNumber::MAX_PARTS - 1; i >= 0; --i)
    {
      if (divisor.digits[i] > 0)
      {
        firstNonZeroDivisor = i;
        break;
      }
    }
    unsigned long long biggerSum = 0;
    if (firstNonZeroBigger>=0)
      biggerSum += bigger.digits[firstNonZeroBigger] * BigNumber::BASE * BigNumber::BASE;
    if (firstNonZeroBigger - 1 >=0)
      biggerSum += bigger.digits[firstNonZeroBigger-1] * BigNumber::BASE;
    if (firstNonZeroBigger - 2 >= 0)
      biggerSum += bigger.digits[firstNonZeroBigger - 2];
    unsigned long long divisorSum = 0;
    if (firstNonZeroDivisor >= 0)
      divisorSum += divisor.digits[firstNonZeroDivisor] * BigNumber::BASE;
    if (firstNonZeroDivisor -1 >= 0)
      divisorSum += divisor.digits[firstNonZeroDivisor-1];
    unsigned long long quotient = biggerSum / (divisorSum+1);
    BigNumber partResult;
    for (int i = firstNonZeroBigger - 1 - firstNonZeroDivisor; i < BigNumber::MAX_PARTS; ++i)
    {
      if (i>=0)
        partResult.digits[i] = quotient % BigNumber::BASE;
      quotient = quotient / BigNumber::BASE;
    }
    result = result + partResult;
    bigger = bigger - (partResult * divisor);
  }
  return result;
}

BigNumber operator %(BigNumber const& first, BigNumber const& divisor)
{
  BigNumber result;
  return first - ((first / divisor) * divisor);
}

void testBigNumbers()
{
  for (long long i = 0; i < 10000; i+=27)
  {
    for (long long j = 0; j < 10000; j+=14)
    {
      BigNumber b1 = fromLongLong(i);
      BigNumber b2 = fromLongLong(j);
      BigNumber sum = b1 + b2;
      std::string sumString = toString(sum);
      if (sumString != std::to_string(i + j))
      {
        std::cout << "ERROR";
      }
      BigNumber product = b1 * b2;
      std::string productString = toString(product);
      if (productString != std::to_string(i * j))
      {
        std::cout << "ERROR";
      }

      if (j>0)
      { 
        BigNumber quotient = b1 / b2;
        std::string quotientString = toString(quotient);
        if (quotientString != std::to_string(i / j))
        {
          std::cout << "ERROR";
          BigNumber quotient2 = b1 / b2;
        }
      }
      if (i < j)
      {
        if (b2 < b1)
        {
          std::cout << "ERROR";
        }
        BigNumber diff = b2 - b1;
        std::string diffString = toString(diff);
        if (diffString != std::to_string(j-i))
        {
          std::cout << "ERROR";
        }
      }
    }
  }
}

struct Fraction
{
  BigNumber numerator;
  BigNumber denominator;
};

BigNumber smallestNWW(BigNumber first, BigNumber second)
{
  BigNumber bigger = first;
  BigNumber smaller = second;
  if (first < second)
  {
    bigger = second;
    smaller = first;
  }
  BigNumber zero;
  while (zero < smaller)
  {
    BigNumber newValue = bigger % smaller;
    bigger = smaller;
    smaller = newValue;
  }
  return bigger;
}

Fraction operator +(Fraction const& first, Fraction const& second)
{
  BigNumber nww = smallestNWW(first.denominator, second.denominator);
  Fraction result;
  result.denominator = first.denominator * second.denominator / nww;
  result.numerator = ((first.denominator * second.numerator) + (first.numerator * second.denominator)) / nww;
  return result;
}

Fraction operator /(Fraction const& first, BigNumber const& second)
{
  BigNumber nww = smallestNWW(first.numerator, second);
  Fraction result;
  result.numerator = first.numerator / nww;
  result.denominator = (first.denominator * second) / nww;
  return result;
}

std::string toString(Fraction bigNumber)
{
  return std::string("[") + toString(bigNumber.numerator) + "/" + toString(bigNumber.denominator) + "]";
}

/*Fraction sum(Fraction& first, Fraction& second)
{
  long long 
}*/

void testFractions()
{
  Fraction f1{fromLongLong(3), fromLongLong(4) };
  Fraction f2{ fromLongLong(1), fromLongLong(6) };
  std::cout << toString(f1+f2) << "\n";
  std::cout << toString(f1 / fromLongLong(6)) << "\n";
}

struct Node
{
  std::vector<int> neigbours;
};

BigNumber solve(std::vector<Node> nodes)
{
  std::vector<Fraction> nodeSize(nodes.size());
  BigNumber nww = fromLongLong(1);
  nodeSize[0] = Fraction{ fromLongLong(1), fromLongLong(1) };
  for (int i=1; i<nodes.size(); ++i)
    nodeSize[i] = Fraction{ fromLongLong(0), fromLongLong(1) };
  for (int i = 0; i < nodes.size(); ++i)
  {
    if (nodeSize[i].numerator == fromLongLong(0))
      continue;
    if (nodes[i].neigbours.size() == 0)
      continue;
    Fraction edgeCount = nodeSize[i] / fromLongLong(nodes[i].neigbours.size());
    nww = nww * edgeCount.denominator / smallestNWW(nww, edgeCount.denominator);
    for (int j = 0; j < nodes[i].neigbours.size(); ++j)
    {
      nodeSize[nodes[i].neigbours[j]] = nodeSize[nodes[i].neigbours[j]] + edgeCount;
    }
  }
  return nww;
}

void testMaxTest()
{
  std::vector<Node> nodes;
  for (int i = 0; i < 100; ++i)
  {
    Node node;
    for (int j = i+1; j < 100; ++j)
    {
      node.neigbours.push_back(j);
    }
    nodes.push_back(node);
  }
  BigNumber result = solve(nodes);
  std::cout << toString(result) << "\n";
}

int main()
{
  const clock_t begin_time = clock();
  int n;
  std::cin >> n;

  std::vector<Node> nodes;
  for (int i = 0; i < n; ++i)
  {
    Node node;
    int neigbourNumber;
    std::cin >> neigbourNumber;
    for (int j = 0; j < neigbourNumber; ++j)
    {
      int neigbour;
      std::cin >> neigbour;
      node.neigbours.push_back(neigbour-1);
    }
    nodes.push_back(node);
  }
  BigNumber result = solve(nodes);
  std::cout << toString(result) << "\n";
  //testMaxTest();
  //testBigNumbers();
  //testFractions();
  //std::cout << float(clock() - begin_time) / CLOCKS_PER_SEC << "\n";
  return 0;
};