#include <bits/stdc++.h>
using namespace std;
void solve(){
int n;cin>>n;
vector<int> v(n+1),pos(n+1);
for(int i=1;i<=n;i++){
cin>>v[i];
pos[v[i]]=i;
}
vector<int> ans;
for(int i=1;i<=n;i++){
vector<int> a(n+1);
a[i]=1;
int flag=1;
for(int j=1;j<=n;j++){
if(j==v[i]){
continue;
}
if(a[j]==0){
a[j]=1;
if(v[j]!=j){
flag=0;
}
}
else if(a[pos[j]]==0){
a[pos[j]]=1;
}
else{
flag=0;
}
}
if(flag){
ans.push_back(i);
}
}
cout<<(ans.size()==1?ans[0]:0)<<endl;
}
int32_t main() {
int t;cin>>t;
while(t--){
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |