#include <iostream> using namespace std; int main() { int n; int c=1; int w =0; cin >> n; int platform[n+1][n]; int conct[n+1]; for(int i=1;i<n+1;i++) { int k=0; cin >> k; if (k!=0) { platform[i][0]=0; conct[i] = k; for(int j=1;j<=k;j++) { cin >> platform[i][j]; } } else { platform[i][0] = 0; platform[i][1] = 0; conct[i] = 1; } } int a = 1; while (c!=0) { c=0; while (platform[a][1] !=0) { int x = a; a = platform[a][platform[a][0]+1]; platform[x][0] = (platform[x][0]+1)%conct[x]; } w++; a=1; for(int i=1;i<=n;i++) { c+=platform[i][0]; } } cout << w; }
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 | #include <iostream> using namespace std; int main() { int n; int c=1; int w =0; cin >> n; int platform[n+1][n]; int conct[n+1]; for(int i=1;i<n+1;i++) { int k=0; cin >> k; if (k!=0) { platform[i][0]=0; conct[i] = k; for(int j=1;j<=k;j++) { cin >> platform[i][j]; } } else { platform[i][0] = 0; platform[i][1] = 0; conct[i] = 1; } } int a = 1; while (c!=0) { c=0; while (platform[a][1] !=0) { int x = a; a = platform[a][platform[a][0]+1]; platform[x][0] = (platform[x][0]+1)%conct[x]; } w++; a=1; for(int i=1;i<=n;i++) { c+=platform[i][0]; } } cout << w; } |