/****************************************************************************** Online C++ Compiler. Code, Compile, Run and Debug C++ program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <iostream> #include <vector> #include <unordered_set> using namespace std; void zasymuluj(vector<vector<int>>& v, vector<int>& stan, unordered_set<int>& tasmy_nz) { int curr = 0; while(1) { if(v[curr].size() == 0) break; int next = v[curr][stan[curr]]; stan[curr] = (stan[curr] + 1) % v[curr].size(); if(stan[curr] == 0) { tasmy_nz.erase(curr); } else { tasmy_nz.insert(curr); } curr = next; } } int solve(vector<vector<int>>& v) { int nr_walizki = 1; unordered_set<int> tasmy_nz; vector<int> stan(v.size(), 0); while(1) { zasymuluj(v, stan, tasmy_nz); if(tasmy_nz.size() == 0) break; nr_walizki++; } return nr_walizki; } int main() { int n; cin >> n; int m; vector<vector<int>> v(n); for(int i = 0; i < n; i++) { cin >> m; v[i].resize(m); for(int j = 0; j < m; j++) { cin >> v[i][j]; v[i][j]--; } } cout << solve(v) << endl; 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 | /****************************************************************************** Online C++ Compiler. Code, Compile, Run and Debug C++ program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <iostream> #include <vector> #include <unordered_set> using namespace std; void zasymuluj(vector<vector<int>>& v, vector<int>& stan, unordered_set<int>& tasmy_nz) { int curr = 0; while(1) { if(v[curr].size() == 0) break; int next = v[curr][stan[curr]]; stan[curr] = (stan[curr] + 1) % v[curr].size(); if(stan[curr] == 0) { tasmy_nz.erase(curr); } else { tasmy_nz.insert(curr); } curr = next; } } int solve(vector<vector<int>>& v) { int nr_walizki = 1; unordered_set<int> tasmy_nz; vector<int> stan(v.size(), 0); while(1) { zasymuluj(v, stan, tasmy_nz); if(tasmy_nz.size() == 0) break; nr_walizki++; } return nr_walizki; } int main() { int n; cin >> n; int m; vector<vector<int>> v(n); for(int i = 0; i < n; i++) { cin >> m; v[i].resize(m); for(int j = 0; j < m; j++) { cin >> v[i][j]; v[i][j]--; } } cout << solve(v) << endl; return 0; } |