#include <iostream> #include <vector> using namespace std; const int MAX_N = 101; vector <int> graf[MAX_N]; int toposort[MAX_N]; int topo_id = 0; bool visited[MAX_N]; long long spos[MAX_N]; long long nwd(long long a, long long b){ if(min(a,b) == 0) return max(a,b); return nwd(max(a,b) % min(a,b), min(a,b)); } long long nww(long long a, long long b){ long long w = a / nwd(a,b); w *= b; return w; } void dfs_topo(int id){ visited[id] = true; for(int i = 0; i < graf[id].size(); i++){ if(visited[graf[id][i]]) continue; dfs_topo(graf[id][i]); } toposort[topo_id] = id; topo_id++; } int fast_read(){ int w = 0; char c = getchar_unlocked(); while(c > '9' or c < '0') c = getchar_unlocked(); while('9'>= c and c >= '0') { w = w * 10 + (c - '0'); c = getchar_unlocked(); } return w; } int main() { int n; n = fast_read(); //m = fast_read(); int a,b,r; for(int i = 0; i < n; i ++){ //cin>>a>>b; r = fast_read(); for(int j = 0; j < r; j++){ a = fast_read(); //b = fast_read(); graf[i + 1].push_back(a); } } dfs_topo(1); for(int i = 1; i <= n; i++){ spos[i] = graf[i].size(); if(spos[i] == 0) spos[i] = 1; } for(int i = topo_id - 1; i >= 0; i--){ for(int j = 0; j < graf[toposort[i]].size(); j++ ){ spos[graf[toposort[i]][j]] = nww(spos[graf[toposort[i]][j]], spos[toposort[i]]); } } long long wyn = 1; for(int i = 1; i <= n; i++ ){ if(visited[i]) wyn = nww(wyn, spos[i]); } cout<<wyn; 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 95 96 97 98 99 100 101 102 103 104 105 | #include <iostream> #include <vector> using namespace std; const int MAX_N = 101; vector <int> graf[MAX_N]; int toposort[MAX_N]; int topo_id = 0; bool visited[MAX_N]; long long spos[MAX_N]; long long nwd(long long a, long long b){ if(min(a,b) == 0) return max(a,b); return nwd(max(a,b) % min(a,b), min(a,b)); } long long nww(long long a, long long b){ long long w = a / nwd(a,b); w *= b; return w; } void dfs_topo(int id){ visited[id] = true; for(int i = 0; i < graf[id].size(); i++){ if(visited[graf[id][i]]) continue; dfs_topo(graf[id][i]); } toposort[topo_id] = id; topo_id++; } int fast_read(){ int w = 0; char c = getchar_unlocked(); while(c > '9' or c < '0') c = getchar_unlocked(); while('9'>= c and c >= '0') { w = w * 10 + (c - '0'); c = getchar_unlocked(); } return w; } int main() { int n; n = fast_read(); //m = fast_read(); int a,b,r; for(int i = 0; i < n; i ++){ //cin>>a>>b; r = fast_read(); for(int j = 0; j < r; j++){ a = fast_read(); //b = fast_read(); graf[i + 1].push_back(a); } } dfs_topo(1); for(int i = 1; i <= n; i++){ spos[i] = graf[i].size(); if(spos[i] == 0) spos[i] = 1; } for(int i = topo_id - 1; i >= 0; i--){ for(int j = 0; j < graf[toposort[i]].size(); j++ ){ spos[graf[toposort[i]][j]] = nww(spos[graf[toposort[i]][j]], spos[toposort[i]]); } } long long wyn = 1; for(int i = 1; i <= n; i++ ){ if(visited[i]) wyn = nww(wyn, spos[i]); } cout<<wyn; return 0; } |