#include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<(int)b;++i) #define FORD(i,a,b) for(int i=a;i>=(int)b;--i) #define PB push_back #define EB emplace_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vvi vector<vi> #define vll vector<ll> #define vvll vector<vll> #define vpii vector<pii> #define pii pair<int, int> #define pll pair<ll, ll> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG #define endl (char)10 #endif using namespace std; using ll = long long; using ld = long double; template <class T> istream& operator>> (istream& is, vector<T>& vec){ FOR(i,0,vec.size()) is >> vec[i]; return is; } template <class T> ostream& operator<< (ostream& os, vector<T>& vec){ for(auto& t : vec) os << t << " "; return os; } template<class T, class U> ostream& operator<< (ostream& os, const pair<T, U>& p){ os << p.FI << " " << p.SE; return os; } template<class T, class U> istream& operator>> (istream& is, pair<T, U>& p){ is >> p.FI >> p.SE; return is; } struct steps { vll v; ll m = 1; }; void expand(steps& s, ll g){ ll d = __gcd(g, s.m); int r = g / d; int l = s.v.size(); FOR(i,1,r){ FOR(j,0,l) s.v.PB(s.m * i + s.v[j]); } s.m *= r; } void add(steps& s, vll& x, ll m){ //cout << "ADD " << s.v << " " << x << "; " << s.m << " " << m << endl; expand(s, m); vll pom; swap(s.v, pom); int i = 0, j = 0; while(i < pom.size() || j < x.size()){ if(i == pom.size()) s.v.PB(x[j++]); else if(j == x.size()) s.v.PB(pom[i++]); else if (pom[i] < x[j]) s.v.PB(pom[i++]); else if (pom[i] == x[j]) { s.v.PB(pom[i++]); j++; } else if (pom[i] > x[j]) s.v.PB(x[j++]); } } ll simulate(vvi& G) { ll u = 0; vi state(G.size(), 0); while(true){ //cout << u << ": "; u++; int c = 0; while(true){ //cout << c + 1 << " "; if (G[c].size() == 0) break; int nc = G[c][state[c]]; state[c]++; if (state[c] == G[c].size()) state[c] = 0; c = nc; } //cout << "; " << state << endl; bool kon = true; FOR(i,0,G.size()) if (state[i] != 0) kon = false; if (kon) break; } return u; } ll lcm(ll a, ll b){ if (a == 0) return b; if (b == 0) return a; return (a / __gcd(a, b)) * b; } ostream& operator<< (ostream& os, steps& s){ os << '{' << s.v << "} " << s.m; return os; } int main () { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<steps> V(n); V[0].v.PB(0); vvi G(n); FOR(i,0,n){ int x; cin >> x; FOR(j,0,x){ int a; cin >> a; G[i].PB(a - 1); } } cout << simulate(G) << endl; /*vll ANS(n); FOR(i,0,n){ ANS[i] = V[i].m; expand(V[i], G[i].size()); FOR(j,0,G[i].size()){ vll pom; for(int k = j; k < V[i].v.size(); k += G[i].size()) pom.PB(V[i].v[k]); add(V[G[i][j]], pom, V[i].m); } } ll ans = 1; FOR(i,0,n) ans = lcm(ans, ANS[i]); FOR(i,0,n) cout << V[i] << endl; cout << ANS << endl; cout << ans << endl;*/ }
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | #include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<(int)b;++i) #define FORD(i,a,b) for(int i=a;i>=(int)b;--i) #define PB push_back #define EB emplace_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vvi vector<vi> #define vll vector<ll> #define vvll vector<vll> #define vpii vector<pii> #define pii pair<int, int> #define pll pair<ll, ll> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG #define endl (char)10 #endif using namespace std; using ll = long long; using ld = long double; template <class T> istream& operator>> (istream& is, vector<T>& vec){ FOR(i,0,vec.size()) is >> vec[i]; return is; } template <class T> ostream& operator<< (ostream& os, vector<T>& vec){ for(auto& t : vec) os << t << " "; return os; } template<class T, class U> ostream& operator<< (ostream& os, const pair<T, U>& p){ os << p.FI << " " << p.SE; return os; } template<class T, class U> istream& operator>> (istream& is, pair<T, U>& p){ is >> p.FI >> p.SE; return is; } struct steps { vll v; ll m = 1; }; void expand(steps& s, ll g){ ll d = __gcd(g, s.m); int r = g / d; int l = s.v.size(); FOR(i,1,r){ FOR(j,0,l) s.v.PB(s.m * i + s.v[j]); } s.m *= r; } void add(steps& s, vll& x, ll m){ //cout << "ADD " << s.v << " " << x << "; " << s.m << " " << m << endl; expand(s, m); vll pom; swap(s.v, pom); int i = 0, j = 0; while(i < pom.size() || j < x.size()){ if(i == pom.size()) s.v.PB(x[j++]); else if(j == x.size()) s.v.PB(pom[i++]); else if (pom[i] < x[j]) s.v.PB(pom[i++]); else if (pom[i] == x[j]) { s.v.PB(pom[i++]); j++; } else if (pom[i] > x[j]) s.v.PB(x[j++]); } } ll simulate(vvi& G) { ll u = 0; vi state(G.size(), 0); while(true){ //cout << u << ": "; u++; int c = 0; while(true){ //cout << c + 1 << " "; if (G[c].size() == 0) break; int nc = G[c][state[c]]; state[c]++; if (state[c] == G[c].size()) state[c] = 0; c = nc; } //cout << "; " << state << endl; bool kon = true; FOR(i,0,G.size()) if (state[i] != 0) kon = false; if (kon) break; } return u; } ll lcm(ll a, ll b){ if (a == 0) return b; if (b == 0) return a; return (a / __gcd(a, b)) * b; } ostream& operator<< (ostream& os, steps& s){ os << '{' << s.v << "} " << s.m; return os; } int main () { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<steps> V(n); V[0].v.PB(0); vvi G(n); FOR(i,0,n){ int x; cin >> x; FOR(j,0,x){ int a; cin >> a; G[i].PB(a - 1); } } cout << simulate(G) << endl; /*vll ANS(n); FOR(i,0,n){ ANS[i] = V[i].m; expand(V[i], G[i].size()); FOR(j,0,G[i].size()){ vll pom; for(int k = j; k < V[i].v.size(); k += G[i].size()) pom.PB(V[i].v[k]); add(V[G[i][j]], pom, V[i].m); } } ll ans = 1; FOR(i,0,n) ans = lcm(ans, ANS[i]); FOR(i,0,n) cout << V[i] << endl; cout << ANS << endl; cout << ans << endl;*/ } |