#include <bits/stdc++.h> using namespace std; vector<int> nexT[101]; vector<int> preV[101]; unsigned long long result[101]; unsigned long long gcd(unsigned long long a, unsigned long long b) { if (b) { return gcd(b, a % b); } else { return a; } } unsigned long long lcm(unsigned long long a, unsigned long long b) { return (a / gcd(a, b)) * b; } int main() { /*for(int i=1;i<=100;i++) { cout << (100-i) << " "; for(int j=i+1;j<=100;j++) { cout << j << " "; } cout << "\n"; } if(true) return 0; */ int n,k,l; cin >> n; for(int i=1;i<=n;i++) { cin >> k; for(int j=0;j<k;j++) { cin >> l; nexT[i].push_back(l); preV[l].push_back(i); } } for(int i=1;i<=n;i++) { if(nexT[i].size() == 0) { result[i] = 0; //cout << "result " << i << ":" << result[i] << "\n"; continue; } else if(preV[i].size() == 0) { if(i==1) { int size = nexT[i].size(); result[i] = max(1, size); //cout << "result " << i << ":" << result[i] << "\n"; } else { result[i] = 0; //cout << "result " << i << ":" << result[i] << "\n"; } continue; } unsigned long long ret = 1; for(int j=0;j<preV[i].size();j++) { int p = preV[i][j]; if(result[p] == 0) { continue; } ret = lcm(ret, result[p]); } result[i] = ret; while(true) { unsigned long long sum = 0; for(int j=0;j<preV[i].size();j++) { int p = preV[i][j]; if(result[p] == 0) { continue; } sum += result[i] / result[p]; //cout << "sum " << i << ":" << sum << "\n"; } if(sum % nexT[i].size() == 0) { break; } result[i] += ret; } //cout << "result " << i << ":" << result[i] << "\n"; } unsigned long long ret = 1; for(int i=1;i<=n;i++) { if(result[i]==0) { result[i] = 1; } ret = lcm(ret, result[i]); } cout << ret; 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 91 92 93 94 | #include <bits/stdc++.h> using namespace std; vector<int> nexT[101]; vector<int> preV[101]; unsigned long long result[101]; unsigned long long gcd(unsigned long long a, unsigned long long b) { if (b) { return gcd(b, a % b); } else { return a; } } unsigned long long lcm(unsigned long long a, unsigned long long b) { return (a / gcd(a, b)) * b; } int main() { /*for(int i=1;i<=100;i++) { cout << (100-i) << " "; for(int j=i+1;j<=100;j++) { cout << j << " "; } cout << "\n"; } if(true) return 0; */ int n,k,l; cin >> n; for(int i=1;i<=n;i++) { cin >> k; for(int j=0;j<k;j++) { cin >> l; nexT[i].push_back(l); preV[l].push_back(i); } } for(int i=1;i<=n;i++) { if(nexT[i].size() == 0) { result[i] = 0; //cout << "result " << i << ":" << result[i] << "\n"; continue; } else if(preV[i].size() == 0) { if(i==1) { int size = nexT[i].size(); result[i] = max(1, size); //cout << "result " << i << ":" << result[i] << "\n"; } else { result[i] = 0; //cout << "result " << i << ":" << result[i] << "\n"; } continue; } unsigned long long ret = 1; for(int j=0;j<preV[i].size();j++) { int p = preV[i][j]; if(result[p] == 0) { continue; } ret = lcm(ret, result[p]); } result[i] = ret; while(true) { unsigned long long sum = 0; for(int j=0;j<preV[i].size();j++) { int p = preV[i][j]; if(result[p] == 0) { continue; } sum += result[i] / result[p]; //cout << "sum " << i << ":" << sum << "\n"; } if(sum % nexT[i].size() == 0) { break; } result[i] += ret; } //cout << "result " << i << ":" << result[i] << "\n"; } unsigned long long ret = 1; for(int i=1;i<=n;i++) { if(result[i]==0) { result[i] = 1; } ret = lcm(ret, result[i]); } cout << ret; return 0; } |