#include <iostream> #include <vector> using namespace std; typedef long long ll; const int N = 101; int n, r, a, tab[N], k, ans; bool g = 0; vector <int> G[N]; void DFS(int w){ if (G[w].size()) DFS(G[w][tab[w]]); else return; if (tab[w] == 0) k--; tab[w]++; if (tab[w]>=(int)G[w].size()){ tab[w] = 0; k++; } } int main(){ ios_base::sync_with_stdio(0); cin>>n; k = n; for (int i=0; i<n; i++){ cin>>r; for (int j=0; j<r; j++){ cin>>a; G[i+1].push_back(a); } } DFS(1); ans = 1; while(k != n){ DFS(1); ans++; } cout<<ans<<"\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 | #include <iostream> #include <vector> using namespace std; typedef long long ll; const int N = 101; int n, r, a, tab[N], k, ans; bool g = 0; vector <int> G[N]; void DFS(int w){ if (G[w].size()) DFS(G[w][tab[w]]); else return; if (tab[w] == 0) k--; tab[w]++; if (tab[w]>=(int)G[w].size()){ tab[w] = 0; k++; } } int main(){ ios_base::sync_with_stdio(0); cin>>n; k = n; for (int i=0; i<n; i++){ cin>>r; for (int j=0; j<r; j++){ cin>>a; G[i+1].push_back(a); } } DFS(1); ans = 1; while(k != n){ DFS(1); ans++; } cout<<ans<<"\n"; } |