#include <iostream> #include <vector> using namespace std; #include <stdio.h> bool isTheSame(int n, vector<int> a, vector<int> b){ for(int i = 0; i < n; i++) if(a[i] != b[i]) return false; return true; } int main(void){ int n, k, tmp, packageLoc, oldLoc, cnt = 0; string s; cin >> n; vector<vector<int> > row(n); vector<int> pointer(n,0); vector<int> origPointer(n,0); for(int i = 0; i < n; i++){ cin >> k; for(int j = 0; j < k; j++){ cin >> tmp; row[i].push_back(tmp - 1); } } for(int i = 0; i < n; i++){ if(row[i].size() == 0) pointer[i] = origPointer[i] = -1; } //printf("done\n"); //for(int i = 0; i < n; i++){ // for(int j = 0; j < row[i].size(); j++) // printf("%d ", row[i][j]); // //printf("\n"); //} //for(int i = 0; i < n; i++) // printf("%d ", pointer[i]); //do a single flow packageLoc = 0; do{ packageLoc = 0; while(pointer[packageLoc] != -1){ oldLoc = packageLoc; packageLoc = row[packageLoc][pointer[packageLoc]]; pointer[oldLoc] = (pointer[oldLoc] + 1) % row[oldLoc].size(); //printf("Package moved from %d to %d\n", oldLoc+1, packageLoc+1); } //printf("Pointers: "); //for(int i = 0; i < n; i++) // printf("%d ", pointer[i]); //printf("\n"); cnt++; }while((!isTheSame(n, pointer, origPointer))); printf("%d", cnt); 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 | #include <iostream> #include <vector> using namespace std; #include <stdio.h> bool isTheSame(int n, vector<int> a, vector<int> b){ for(int i = 0; i < n; i++) if(a[i] != b[i]) return false; return true; } int main(void){ int n, k, tmp, packageLoc, oldLoc, cnt = 0; string s; cin >> n; vector<vector<int> > row(n); vector<int> pointer(n,0); vector<int> origPointer(n,0); for(int i = 0; i < n; i++){ cin >> k; for(int j = 0; j < k; j++){ cin >> tmp; row[i].push_back(tmp - 1); } } for(int i = 0; i < n; i++){ if(row[i].size() == 0) pointer[i] = origPointer[i] = -1; } //printf("done\n"); //for(int i = 0; i < n; i++){ // for(int j = 0; j < row[i].size(); j++) // printf("%d ", row[i][j]); // //printf("\n"); //} //for(int i = 0; i < n; i++) // printf("%d ", pointer[i]); //do a single flow packageLoc = 0; do{ packageLoc = 0; while(pointer[packageLoc] != -1){ oldLoc = packageLoc; packageLoc = row[packageLoc][pointer[packageLoc]]; pointer[oldLoc] = (pointer[oldLoc] + 1) % row[oldLoc].size(); //printf("Package moved from %d to %d\n", oldLoc+1, packageLoc+1); } //printf("Pointers: "); //for(int i = 0; i < n; i++) // printf("%d ", pointer[i]); //printf("\n"); cnt++; }while((!isTheSame(n, pointer, origPointer))); printf("%d", cnt); return 0; } |