#include<bits/stdc++.h>
// #include<math.h>
using namespace std;
long long mgcd(long long int a, long long int b){
if (b == 0)
return a;
return mgcd(b, a % b);
}
// Function to return LCM of two numbers
long long mlcm2(int a, int b)
{
return (a / mgcd(a, b)) * b;
}
int mlcm(int a, int b){
if (a==0) a=1;
if (b==0) b=1;
return mlcm2(a,b);
}
int main()
{
ios_base::sync_with_stdio(NULL);
cin.tie(0);
cout.tie(0);
// freopen("input.in", "r", stdin);
int n;
cin>>n;
vector<vector<int>>wejscia (n+7);
vector<vector<int>>wyjscia (n+7);
vector<int> nww(n+7);
vector<int> impulsy(n+7);
int a,b;
for (int i = 0; i < n; i++){
cin>>a;
for(int j = 0; j < a; j++){
cin>>b;
wejscia[b-1].push_back(i);
wyjscia[i].push_back(b-1);
}
}
nww[0]=wyjscia[0].size();
int tmp;
for (int i = 1; i < n; i++){
tmp=0;
int max_nww=0;
int suma=0;
int iloczyn=1;
int tmp_imp=0;
for(int j=0; j<wejscia[i].size(); j++){
// iloczyn=iloczyn+nww[wejscia[i][j]];
max_nww=max(max_nww, nww[wejscia[i][j]]);
tmp=mlcm(tmp, nww[wejscia[i][j]]);
tmp_imp=mlcm(tmp_imp, nww[wejscia[i][j]]);
;
}
for(int j=0; j<wejscia[i].size(); j++){
// iloczyn=iloczyn+nww[wejscia[i][j]];
if (nww[wejscia[i][j]])
suma+=max_nww/nww[wejscia[i][j]];
;
}
// nww[i]=mlcm(tmp, wyjscia[i].size());
// nww[i]=tmp;
// for(int j=0; j<wejscia[i].size(); j++){
// if (nww[wejscia[i][j]])
// suma+=tmp/nww[wejscia[i][j]];
// ;
// }
if (suma){
tmp=mlcm(suma,wyjscia[i].size())/suma;
nww[i]=max_nww*tmp;
// tmp=mlcm(suma,wyjscia[i].size());
// nww[i]=mlcm(max_nww,tmp);
}
// else
// nww[i]=wyjscia[i].size()*tmp;
}
tmp=0;
for(auto e:nww){
tmp=mlcm(tmp,e);
}
cout<<tmp;
return 0;
}
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 | #include<bits/stdc++.h> // #include<math.h> using namespace std; long long mgcd(long long int a, long long int b){ if (b == 0) return a; return mgcd(b, a % b); } // Function to return LCM of two numbers long long mlcm2(int a, int b) { return (a / mgcd(a, b)) * b; } int mlcm(int a, int b){ if (a==0) a=1; if (b==0) b=1; return mlcm2(a,b); } int main() { ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0); // freopen("input.in", "r", stdin); int n; cin>>n; vector<vector<int>>wejscia (n+7); vector<vector<int>>wyjscia (n+7); vector<int> nww(n+7); vector<int> impulsy(n+7); int a,b; for (int i = 0; i < n; i++){ cin>>a; for(int j = 0; j < a; j++){ cin>>b; wejscia[b-1].push_back(i); wyjscia[i].push_back(b-1); } } nww[0]=wyjscia[0].size(); int tmp; for (int i = 1; i < n; i++){ tmp=0; int max_nww=0; int suma=0; int iloczyn=1; int tmp_imp=0; for(int j=0; j<wejscia[i].size(); j++){ // iloczyn=iloczyn+nww[wejscia[i][j]]; max_nww=max(max_nww, nww[wejscia[i][j]]); tmp=mlcm(tmp, nww[wejscia[i][j]]); tmp_imp=mlcm(tmp_imp, nww[wejscia[i][j]]); ; } for(int j=0; j<wejscia[i].size(); j++){ // iloczyn=iloczyn+nww[wejscia[i][j]]; if (nww[wejscia[i][j]]) suma+=max_nww/nww[wejscia[i][j]]; ; } // nww[i]=mlcm(tmp, wyjscia[i].size()); // nww[i]=tmp; // for(int j=0; j<wejscia[i].size(); j++){ // if (nww[wejscia[i][j]]) // suma+=tmp/nww[wejscia[i][j]]; // ; // } if (suma){ tmp=mlcm(suma,wyjscia[i].size())/suma; nww[i]=max_nww*tmp; // tmp=mlcm(suma,wyjscia[i].size()); // nww[i]=mlcm(max_nww,tmp); } // else // nww[i]=wyjscia[i].size()*tmp; } tmp=0; for(auto e:nww){ tmp=mlcm(tmp,e); } cout<<tmp; return 0; } |
English