#include <iostream>
#include <vector>
using namespace std;
/**
Helpful links
lcm https://www.geeksforgeeks.org/program-to-find-lcm-of-two-numbers/
*/
void display(const vector<int>& v)
{
for (int i = 0; i < v.size(); ++i)
printf("%d ", v[i]);
printf("\n");
}
// Recursive function to return gcd of a and b
long long gcd(long long int a, long long int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to return LCM of two numbers
long long lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, r, l;
cin >> n;
vector<bool> visited_platforms(n+1);
int nww = 1;
visited_platforms[1] = true;
for (int i = 1; i <= n; i++)
{
cin >> r;
bool is_current_visited = visited_platforms[i];
if (is_current_visited)
{
if (r == 0)
{
continue;
}
nww = lcm(nww, r);
}
for (int j = 0; j < r; j++)
{
cin >> l;
if (is_current_visited)
{
visited_platforms[l] = true;
}
}
}
cout << nww;
}
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 | #include <iostream> #include <vector> using namespace std; /** Helpful links lcm https://www.geeksforgeeks.org/program-to-find-lcm-of-two-numbers/ */ void display(const vector<int>& v) { for (int i = 0; i < v.size(); ++i) printf("%d ", v[i]); printf("\n"); } // Recursive function to return gcd of a and b long long gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } // Function to return LCM of two numbers long long lcm(int a, int b) { return (a / gcd(a, b)) * b; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, r, l; cin >> n; vector<bool> visited_platforms(n+1); int nww = 1; visited_platforms[1] = true; for (int i = 1; i <= n; i++) { cin >> r; bool is_current_visited = visited_platforms[i]; if (is_current_visited) { if (r == 0) { continue; } nww = lcm(nww, r); } for (int j = 0; j < r; j++) { cin >> l; if (is_current_visited) { visited_platforms[l] = true; } } } cout << nww; } |
English