#include<bits/stdc++.h> using namespace std; vector<int> g[102]; vector<int> wiel[102]; int w[102], wch[102], indeksy[102], tr[102]; int n; void wypisz(){ for(int i = 1; i <= n; i++){ cout << i <<" : "; for(int j = 0; j < wiel[i].size(); j++) { cout << wiel[i][j] <<" "; } cout << endl; } } int main() { ios_base::sync_with_stdio(0); int i, j, k, a, trasy = 0, ost=0; cin >> n; for(i = 1; i <= n; i++){ cin >> k; for(j = 1; j <= k; j++) { cin >> a; wch[a]++; g[i].push_back(a); //wiel[i].push_back(0); } } //wypisz(); if(g[1].size() == 0 ) { cout << 1 ; return 0; } int il = 0,dl_trasy; i = 1; while(g[i].size() > 0){ int ind = 0; dl_trasy++; //wiel[i][ind]++; if (g[i].size()>0 )indeksy[i] = (indeksy[i] + 1) % g[i].size(); tr[g[i][ind]]=1; i = g[i][ind]; } //cout << "dl "<<dl_trasy << endl; while(il < dl_trasy){ i = 1; il = 0; while(g[i].size() > 0){ int ind = indeksy[i]; //cout << "indeks " << ind << endl; if(tr[g[i][ind]] == 1) il++; //wiel[i][ind]++; if (g[i].size()>0 )indeksy[i] = (indeksy[i] + 1) % g[i].size(); i = g[i][ind]; //cout <<"il " << " "; } trasy++; } cout << trasy; 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 | #include<bits/stdc++.h> using namespace std; vector<int> g[102]; vector<int> wiel[102]; int w[102], wch[102], indeksy[102], tr[102]; int n; void wypisz(){ for(int i = 1; i <= n; i++){ cout << i <<" : "; for(int j = 0; j < wiel[i].size(); j++) { cout << wiel[i][j] <<" "; } cout << endl; } } int main() { ios_base::sync_with_stdio(0); int i, j, k, a, trasy = 0, ost=0; cin >> n; for(i = 1; i <= n; i++){ cin >> k; for(j = 1; j <= k; j++) { cin >> a; wch[a]++; g[i].push_back(a); //wiel[i].push_back(0); } } //wypisz(); if(g[1].size() == 0 ) { cout << 1 ; return 0; } int il = 0,dl_trasy; i = 1; while(g[i].size() > 0){ int ind = 0; dl_trasy++; //wiel[i][ind]++; if (g[i].size()>0 )indeksy[i] = (indeksy[i] + 1) % g[i].size(); tr[g[i][ind]]=1; i = g[i][ind]; } //cout << "dl "<<dl_trasy << endl; while(il < dl_trasy){ i = 1; il = 0; while(g[i].size() > 0){ int ind = indeksy[i]; //cout << "indeks " << ind << endl; if(tr[g[i][ind]] == 1) il++; //wiel[i][ind]++; if (g[i].size()>0 )indeksy[i] = (indeksy[i] + 1) % g[i].size(); i = g[i][ind]; //cout <<"il " << " "; } trasy++; } cout << trasy; return 0; } |