1
0

CodeForces-1270G.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4. ios::sync_with_stdio(false);
  5. cin.tie(nullptr);
  6. int tests;
  7. cin >> tests;
  8. while (tests--) {
  9. int n;
  10. cin >> n;
  11. vector<int> next(n), indegree(n, 0);
  12. for (int i = 0; i < n; ++i) {
  13. int value;
  14. cin >> value;
  15. next[i] = i - value;
  16. ++indegree[next[i]];
  17. }
  18. queue<int> leaves;
  19. for (int i = 0; i < n; ++i) if (indegree[i] == 0) leaves.push(i);
  20. while (!leaves.empty()) {
  21. int v = leaves.front();
  22. leaves.pop();
  23. if (--indegree[next[v]] == 0) leaves.push(next[v]);
  24. }
  25. int start = 0;
  26. while (indegree[start] == 0) ++start;
  27. vector<int> cycle;
  28. int v = start;
  29. do {
  30. cycle.push_back(v);
  31. v = next[v];
  32. } while (v != start);
  33. cout << cycle.size() << '\n';
  34. for (int x : cycle) cout << x + 1 << ' ';
  35. cout << '\n';
  36. }
  37. return 0;
  38. }