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
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <tuple>
#include <stack>
#include <iomanip>
using namespace std;

class poorbignum{
    vector<uint64_t> data;
    static const uint64_t M = 1000'000'000;
public:
    poorbignum(int64_t x){
        do{
            data.push_back(x%M);
            x/=M;
        }while(x>0);
    };
    void trim(){
        while(data.back()==0){
            data.pop_back();
        }
    }
    poorbignum(){
        data.push_back(0);

    }

    friend ostream& operator<<(ostream& os, const poorbignum& x);

    poorbignum operator*=(const uint64_t other){
        //other < 10^9
        uint64_t aku=0;

        for (int i=0; i<data.size();i++){
            aku = data[i]*other+aku;
            data[i] = aku%M;
            aku /= M;
        }
        while (aku>0){
            data.push_back(aku%M);
            aku /= M;
        }
        return *this;
    }
    poorbignum operator+=(const uint64_t other){
        //other < 10^9
        uint64_t aku=other;

        for (int i=0; i<data.size();i++){
            aku = data[i]+aku;
            data[i] = aku%M;
            aku /= M;
        }
        while (aku>0){
            data.push_back(aku%M);
            aku /= M;
        }
        return *this;
    }
    poorbignum operator+=(const poorbignum &other){
        //other < 10^9
        uint64_t aku=0;

        if (other.data.size() > data.size() )
            data.resize( other.data.size(), 0 );
        int i=0;
        for (; i<other.data.size();i++){
            aku = data[i]+aku+ other.data[i];
            data[i] = aku%M;
            aku /= M;
        }

        for (; i<data.size();i++){
            aku = data[i]+aku;
            data[i] = aku%M;
            aku /= M;
        }

        while (aku>0){
            data.push_back(aku%M);
            aku /= M;
        }
        return *this;
    }


    poorbignum operator/=(const uint64_t other){
        //other < 10^9
        uint64_t aku=0;

        for (int i=data.size()-1; i>=0;i--){
            aku = data[i]+aku*M;
            data[i] = aku/other;
            aku %= other;
        }
        trim();
        return *this;
    }
    uint64_t rem(const uint64_t other){
        uint64_t aku=0;
        for (int i=data.size()-1; i>=0;i--){
            aku = data[i]+aku*M;
            aku %= other;
        }
        return aku;
    }


};

ostream& operator<<(ostream& os, const poorbignum& x)
{
    os<<x.data.back();
    for (int i=x.data.size()-2; i>=0; i--)
        os<<std::setfill('0') << std::setw(9) <<x.data[i];
    return os;
}

poorbignum cykl=1;
vector<poorbignum> odwiedzen;
vector<poorbignum> odw_tasm;

vector<vector<int>> wych;
vector<vector<int>> przy;


int64_t GCD( int64_t x, int64_t y ){
    //x<y
    if (x==0)
        return y;
    else
        return GCD(y%x,x);
}

int64_t LCM( int64_t x, int64_t y ){
    if (x>y) swap(x,y);
    return x*y/GCD(x,y);
}

void przetworz (int v){
    poorbignum wejsc(0);//wejsc na cykl

    for (auto i: przy[v] ){
        wejsc += odw_tasm[i];
    }
    int64_t rzad = wych[v].size();
    if (rzad==0) return;
    uint64_t remwejsc = wejsc.rem(rzad);
    if (remwejsc==0){
        odwiedzen[v]=wejsc;
        odw_tasm [v] = wejsc;
        odw_tasm [v] /= rzad;
        return;
    }
    int64_t mnoznik = rzad/GCD(remwejsc, rzad);
    wejsc *=mnoznik;
    cykl*=mnoznik;
    for (int i=0; i<v; i++){
        odwiedzen[i]*=mnoznik;
        odw_tasm[i]*=mnoznik;
    }
    odwiedzen[v]=wejsc;
    if (!(wejsc.rem(rzad)==0)){
        cout<<"dupa"<<endl;
    }
    odw_tasm[v]=wejsc;
    odw_tasm[v]/=rzad;

}

int main(){

    int n;
    cin>>n;
    wych = vector<vector<int>>(n) ;
    przy = vector<vector<int>>(n) ;

    for (int x=0; x<n;x++){
        int m;
        cin>>m;
        for (int i=0; i<m; i++){
            int y;
            cin>>y;
            y--;
            wych[x].push_back(y);
            przy[y].push_back(x);
        }
    }

    odwiedzen = vector<poorbignum> (n,0);
    odw_tasm = vector<poorbignum> (n,0);

    odwiedzen[0] = wych[0].size();
    odw_tasm[0]=1;
    cykl = max<int>(wych[0].size(),1);
    for (int v=1; v<n; v++){
        przetworz(v);
    }

    cout << cykl << endl;
    return 0;
}