#include <bits/stdc++.h> using namespace std; constexpr int MAXN = 1007; vector <int> dokad[MAXN]; vector <int> ktore; void DFS(int w) { ktore.push_back(w-1); int sise = dokad[w-1].size(); for(int i = 0; i < sise; ++i) { DFS(dokad[w-1][i]); } } int NWD(int a, int b) { int r; while(r != 0) { r = a % b; a = b; b = r; } return a; } int NWW(int a, int b) { if(a < b) return NWW(b, a); if(b == 0) return a; int ab = a*b; return ab/NWD(a, b); } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); int n; cin >> n; for(int i = 0; i < n; ++i) { int a; cin >> a; for(int j = 0; j < a; ++j) { int x; cin >> x; dokad[i].push_back(x); } } DFS(1); int sise = ktore.size(); int odp = dokad[ktore[0]].size(); cerr << dokad[ktore[0]].size() << "\n"; //cerr << odp << "\n"; for(int i = 1; i < sise; ++i) { odp = NWW(odp, dokad[ktore[i]].size()); cerr << dokad[ktore[i]].size() << "\n"; //cerr << odp << "\n"; } if(odp == 0) { cout << 1; return 0; } cout << odp; }
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 | #include <bits/stdc++.h> using namespace std; constexpr int MAXN = 1007; vector <int> dokad[MAXN]; vector <int> ktore; void DFS(int w) { ktore.push_back(w-1); int sise = dokad[w-1].size(); for(int i = 0; i < sise; ++i) { DFS(dokad[w-1][i]); } } int NWD(int a, int b) { int r; while(r != 0) { r = a % b; a = b; b = r; } return a; } int NWW(int a, int b) { if(a < b) return NWW(b, a); if(b == 0) return a; int ab = a*b; return ab/NWD(a, b); } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); int n; cin >> n; for(int i = 0; i < n; ++i) { int a; cin >> a; for(int j = 0; j < a; ++j) { int x; cin >> x; dokad[i].push_back(x); } } DFS(1); int sise = ktore.size(); int odp = dokad[ktore[0]].size(); cerr << dokad[ktore[0]].size() << "\n"; //cerr << odp << "\n"; for(int i = 1; i < sise; ++i) { odp = NWW(odp, dokad[ktore[i]].size()); cerr << dokad[ktore[i]].size() << "\n"; //cerr << odp << "\n"; } if(odp == 0) { cout << 1; return 0; } cout << odp; } |