#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define yes cout<<"YES"<<endl;
#define endl "\n"
const int N=2e5+10;
const int mod=998244353;
const ll inf=0x3f3f3f3f3f3f3f3f;
int a[N],vis[N],tag[N];
int flag=1,cnt=0;
void dfs(int u)
{
if(tag[a[u]])
{
if(tag[a[u]]!=3-tag[u])
{
flag=0;
}
return ;
}
else
{
tag[a[u]]=3-tag[u];
dfs(a[u]);
}
}
void solve()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
vis[a[i]]++;
}
queue<int>q;
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
q.push(i);
//cout<<i<<endl;
}
}
while(!q.empty())
{
int u=q.front();
q.pop();
tag[u]=1;
if(!tag[a[u]]&&--vis[a[a[u]]]==0)
{
q.push(a[a[u]]);
}
tag[a[u]]=2;
}
for(int i=1;i<=n;i++)
{
//cout<<tag[i]<<" \n"[i==n];
if(!tag[i])tag[i]=1,dfs(i);
}
if(!flag)
{
cout<<"-1\n";
}
else
{
vector<int>ans;
for(int i=1;i<=n;i++)
{
if(tag[i]==1)
{
ans.push_back(i);
}
}
cout<<(int)ans.size()<<"\n";
for(int i:ans)
{
cout<<a[i]<<" ";
}
cout<<"\n";
}
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);
int _=1;
//cin>>_;
while(_--)
{
solve();
}
}
275. H-Index II | 274. H-Index |
260. Single Number III | 240. Search a 2D Matrix II |
238. Product of Array Except Self | 229. Majority Element II |
222. Count Complete Tree Nodes | 215. Kth Largest Element in an Array |
198. House Robber | 153. Find Minimum in Rotated Sorted Array |
150. Evaluate Reverse Polish Notation | 144. Binary Tree Preorder Traversal |
137. Single Number II | 130. Surrounded Regions |
129. Sum Root to Leaf Numbers | 120. Triangle |
102. Binary Tree Level Order Traversal | 96. Unique Binary Search Trees |
75. Sort Colors | 74. Search a 2D Matrix |
71. Simplify Path | 62. Unique Paths |
50. Pow(x, n) | 43. Multiply Strings |
34. Find First and Last Position of Element in Sorted Array | 33. Search in Rotated Sorted Array |
17. Letter Combinations of a Phone Number | 5. Longest Palindromic Substring |
3. Longest Substring Without Repeating Characters | 1312. Minimum Insertion Steps to Make a String Palindrome |