#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,r,l;
vector<int> graf[105];
int state[105];
void DFS(int x)
{
if(graf[x].empty()) return;
int akt = graf[x][state[x]];
state[x]++;
state[x]%=graf[x].size();
DFS(akt);
}
bool state0(){
for(int i=1; i<=n; i++)
if(state[i]) return false;
return true;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n;
for(int i=1; i<=n; i++){
cin>>r;
for(int j=0; j<r; j++){
cin>>l;
graf[i].push_back(l);
}
}
for(int i=1;;i++){
DFS(1);
if(state0()){
cout<<i;
break;
}
// cout<<"i: "<<i<<'\n';
//for(int j=1; j<=n; j++)cout<<state[j]<<' ';cout<<'\n';
}
}
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; int n,r,l; vector<int> graf[105]; int state[105]; void DFS(int x) { if(graf[x].empty()) return; int akt = graf[x][state[x]]; state[x]++; state[x]%=graf[x].size(); DFS(akt); } bool state0(){ for(int i=1; i<=n; i++) if(state[i]) return false; return true; } int main(){ ios_base::sync_with_stdio(0);cin.tie(0); cin>>n; for(int i=1; i<=n; i++){ cin>>r; for(int j=0; j<r; j++){ cin>>l; graf[i].push_back(l); } } for(int i=1;;i++){ DFS(1); if(state0()){ cout<<i; break; } // cout<<"i: "<<i<<'\n'; //for(int j=1; j<=n; j++)cout<<state[j]<<' ';cout<<'\n'; } } |
English