Advertisement
Guest User

Untitled

a guest
Dec 29th, 2019
1,932
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. void solve()
  6. {
  7. int n;
  8. cin >> n;
  9. vector<int> a(n+1);
  10. for (int i = 1; i <= n; i++) cin >> a[i];
  11. vector<bool> visited(n+1);
  12. int cur = 1;
  13. while (!visited[cur])
  14. {
  15. visited[cur] = true;
  16. cur = cur - a[cur];
  17. }
  18. vector<int> answer = {cur};
  19. int cur1 = cur - a[cur];
  20. while (cur1!=cur)
  21. {
  22. answer.push_back(cur1);
  23. cur1 = cur1 - a[cur1];
  24. }
  25. cout<<answer.size()<<'\n';
  26. for (auto it: answer) cout<<it<<' ';
  27. cout<<'\n';
  28. }
  29.  
  30. int main() {
  31. ios_base::sync_with_stdio(0);
  32. cin.tie(nullptr);
  33.  
  34. int t;
  35. cin>>t;
  36. for (int i = 0; i<t; i++) solve();
  37.  
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement