constint maxn = 1e6 + 10; int n, in[maxn], col[maxn], to[maxn]; std::queue<int> q; std::vector<int> ans;
tsz main(){ read(n); for (int i = 1; i <= n; i++) { read(to[i]); in[to[i]]++; } memset(col, -1, sizeof(col)); for (int i = 1; i <= n; i++) { if (!in[i]) q.push(i), col[i] = 0; } while (!q.empty()) { int u = q.front(); q.pop(); if (!col[u]) col[to[u]] = 1; in[to[u]]--; if (in[to[u]] <= 0) { if (!(~col[to[u]])) col[to[u]] = 0; q.push(to[u]); } } int rt = -1; for (int i = 1; i <= n; i++) { if (in[i] > 0) { rt = -1; int u = i; while (to[u] != i) { if (~col[u]) { rt = u; } u = to[u]; } if (~col[u]) { rt = u; } if (!(~rt)) { col[i] = 0; rt = i; } // 找环上有颜色的起点,找不到任选起点
u = rt; in[u]--; while (to[u] != rt) { if (!col[u]) { col[to[u]] = 1; } elseif (!(~col[to[u]])) { col[to[u]] = 0; } in[to[u]]--; u = to[u]; } if (!col[u] && !col[to[u]]) { puts("-1"); return0; } } } for (int i = 1; i <= n; i++) { if (!col[i]) { ans.push_back(to[i]); } } writeln(ans.size()); for (int e : ans) { write(e); putchar(' '); } puts(""); return0; }