#include <iostream>
#include <vector>
int main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int n; std::cin >> n;
std::vector<int> graph[n];
for (int i = 0; i < n; i++)
{
int r; std::cin >> r;
for (int j = 0; j < r; j++)
{
int l; std::cin >> l;
graph[i].push_back(l - 1);
}
}
int A[n]; for (int i = 0; i < n; i++) { A[i] = 0; }
int result = 0;
bool next = true;
int current; int temp;
while (next)
{
result++;
current = 0;
while (graph[current].size() != 0)
{
temp = current;
current = graph[current][A[current]];
A[temp] = (A[temp] + 1) % graph[temp].size();
}
next = false;
for (int i = 0; i < n; i++)
if (A[i] != 0) { next = true; break; }
}
std::cout << result;
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 | #include <iostream> #include <vector> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n; std::cin >> n; std::vector<int> graph[n]; for (int i = 0; i < n; i++) { int r; std::cin >> r; for (int j = 0; j < r; j++) { int l; std::cin >> l; graph[i].push_back(l - 1); } } int A[n]; for (int i = 0; i < n; i++) { A[i] = 0; } int result = 0; bool next = true; int current; int temp; while (next) { result++; current = 0; while (graph[current].size() != 0) { temp = current; current = graph[current][A[current]]; A[temp] = (A[temp] + 1) % graph[temp].size(); } next = false; for (int i = 0; i < n; i++) if (A[i] != 0) { next = true; break; } } std::cout << result; return 0; } |
English