#include <bits/stdc++.h> using namespace std; int T[101][2]; //licznik,mianownik long long NWD(long long a,long long b){ if (a < b)swap(a,b); while (b != 0){ a = a % b; if (a < b)swap(a,b); } return a; } vector <int> Con[101]; pair <int,int> dodaj(pair<int,int> x,pair<int,int> y){ long long licz = x.first * y.second + y.first * x.second; long long mian = x.second*y.second; long long nwd = NWD(licz,mian); licz /= nwd; mian /= nwd; //cout << x.first << " " << x.second << " " << y.first << " " << y.second << " " << licz << " " << x.first << " " << endl; return {licz,mian}; } int main(){ int n; scanf("%d",&n); for (int i = 1; i <= n;i++){ int temp1,temp2; scanf("\n"); scanf("%d",&temp1); for (int j = 0; j < temp1;j++){ scanf("%d",&temp2); Con[i].push_back(temp2); } } T[1][0]=1; T[1][1]=1; long mianownik = 1; for (int i = 1;i <= n;i++){ int x = Con[i].size(); if (T[i][0] != 0 && x != 0){ //cout << i <<" "<<T[i][0] <<" " <<T[i][1] <<" "<<mianownik <<" "<< NWD(T[i][1],mianownik) << endl; mianownik *= (T[i][1]*x/NWD(T[i][1]*x,mianownik)); for (int j = 0; j < Con[i].size();j++){ if (T[Con[i][j]][0] == 0){ T[Con[i][j]][0] = T[i][0]; T[Con[i][j]][1] = T[i][1]*x; int nwd = NWD(T[Con[i][j]][0],T[Con[i][j]][1]); T[Con[i][j]][0] /= nwd; T[Con[i][j]][1] /= nwd; } else{ pair<int,int> m =dodaj({T[Con[i][j]][0],T[Con[i][j]][1]},{T[i][0],T[i][1]*x}); T[Con[i][j]][0] = m.first; T[Con[i][j]][1] = m.second; } } } } cout << mianownik; }
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 | #include <bits/stdc++.h> using namespace std; int T[101][2]; //licznik,mianownik long long NWD(long long a,long long b){ if (a < b)swap(a,b); while (b != 0){ a = a % b; if (a < b)swap(a,b); } return a; } vector <int> Con[101]; pair <int,int> dodaj(pair<int,int> x,pair<int,int> y){ long long licz = x.first * y.second + y.first * x.second; long long mian = x.second*y.second; long long nwd = NWD(licz,mian); licz /= nwd; mian /= nwd; //cout << x.first << " " << x.second << " " << y.first << " " << y.second << " " << licz << " " << x.first << " " << endl; return {licz,mian}; } int main(){ int n; scanf("%d",&n); for (int i = 1; i <= n;i++){ int temp1,temp2; scanf("\n"); scanf("%d",&temp1); for (int j = 0; j < temp1;j++){ scanf("%d",&temp2); Con[i].push_back(temp2); } } T[1][0]=1; T[1][1]=1; long mianownik = 1; for (int i = 1;i <= n;i++){ int x = Con[i].size(); if (T[i][0] != 0 && x != 0){ //cout << i <<" "<<T[i][0] <<" " <<T[i][1] <<" "<<mianownik <<" "<< NWD(T[i][1],mianownik) << endl; mianownik *= (T[i][1]*x/NWD(T[i][1]*x,mianownik)); for (int j = 0; j < Con[i].size();j++){ if (T[Con[i][j]][0] == 0){ T[Con[i][j]][0] = T[i][0]; T[Con[i][j]][1] = T[i][1]*x; int nwd = NWD(T[Con[i][j]][0],T[Con[i][j]][1]); T[Con[i][j]][0] /= nwd; T[Con[i][j]][1] /= nwd; } else{ pair<int,int> m =dodaj({T[Con[i][j]][0],T[Con[i][j]][1]},{T[i][0],T[i][1]*x}); T[Con[i][j]][0] = m.first; T[Con[i][j]][1] = m.second; } } } } cout << mianownik; } |