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
#include <bits/stdc++.h>
using namespace std;

int n;
vector <int> graf[102];
string wynik;
string dp[102];

int reduce(int a, string b)
{
    //https://www.geeksforgeeks.org/gcd-of-two-numbers-when-one-of-them-can-be-very-large-2/
    int mod = 0;
    for (int i = 0; i < b.size(); i++)
        mod = (mod * 10 + b[i] - '0') % a;

    return mod;
}

string longDivision(string number, int divisor)
{
    //https://www.geeksforgeeks.org/divide-large-number-represented-string/
    string ans;

    int idx = 0;
    int temp = number[idx] - '0';
    while (temp < divisor)
        temp = temp * 10 + (number[++idx] - '0');

    while (number.size() > idx) {
        ans += (temp / divisor) + '0';

        temp = (temp % divisor) * 10 + number[++idx] - '0';
    }
     if (ans.length() == 0)
        return "0";

    return ans;
}

string findSum(string str1, string str2)
{
    //https://www.geeksforgeeks.org/sum-two-large-numbers/
    if (str1.length() > str2.length())
        swap(str1, str2);

    string str = "";

    int n1 = str1.length(), n2 = str2.length();

    reverse(str1.begin(), str1.end());
    reverse(str2.begin(), str2.end());

    int carry = 0;
    for (int i=0; i<n1; i++)
    {
        int sum = ((str1[i]-'0')+(str2[i]-'0')+carry);
        str.push_back(sum%10 + '0');

        carry = sum/10;
    }

    for (int i=n1; i<n2; i++)
    {
        int sum = ((str2[i]-'0')+carry);
        str.push_back(sum%10 + '0');
        carry = sum/10;
    }

    if (carry)
        str.push_back(carry+'0');

    reverse(str.begin(), str.end());

    return str;
}

string mnozenie (string s, int l)
{
    string w = "0";
    for(int i=1;i<=l;i++)
    {
        string v = w;
        w=findSum(v,s);
    }
    return w;
}

int main()
{
    for(int i=1;i<=100;i++)dp[i]="0";

    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int r;
        cin>>r;
        for(int j=1;j<=r;j++)
        {
            int a;
            cin>>a;
            graf[i].push_back(a);
        }
    }
    if(graf[1].size()==0)
    {
        cout<<"1";
        return 0;
    }

    wynik = to_string(graf[1].size());
    for(int i:graf[1])dp[i]="1";

    for(int i=2;i<=n;i++)
    {
        if(graf[i].size()==0)
        {
            continue;
        }

        int b = reduce(graf[i].size(),dp[i]);
        int d;
        if(b==0) d = graf[i].size();
        else d = __gcd(b,(int)graf[i].size());
        d = graf[i].size()/d;

        for(int j = i;j<=n;j++)
        {
            string a = dp[j];
            dp[j]=mnozenie(a,d);
        }

        string a = wynik;
        wynik=mnozenie(a,d);
        a = longDivision(dp[i],graf[i].size());

        for(int j:graf[i])
        {
            string k = dp[j];
            dp[j]=findSum(k,a);
            //dp[j]+=dp[i]/graf[i].size();
        }
    }

    cout<<wynik;
}