| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- int tests;
- cin >> tests;
- while (tests--) {
- int n;
- cin >> n;
- vector<int> next(n), indegree(n, 0);
- for (int i = 0; i < n; ++i) {
- int value;
- cin >> value;
- next[i] = i - value;
- ++indegree[next[i]];
- }
- queue<int> leaves;
- for (int i = 0; i < n; ++i) if (indegree[i] == 0) leaves.push(i);
- while (!leaves.empty()) {
- int v = leaves.front();
- leaves.pop();
- if (--indegree[next[v]] == 0) leaves.push(next[v]);
- }
- int start = 0;
- while (indegree[start] == 0) ++start;
- vector<int> cycle;
- int v = start;
- do {
- cycle.push_back(v);
- v = next[v];
- } while (v != start);
- cout << cycle.size() << '\n';
- for (int x : cycle) cout << x + 1 << ' ';
- cout << '\n';
- }
- return 0;
- }
|