#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
int p[maxn];
int ori,cnt;
vector<int> vec;
bool vis[maxn];
int find(int x){
vis[x] = 1,++cnt;
if(p[x]==ori) return cnt;
else find(p[x]);
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--){
int N;
while(cin>>N){
for(int i=1;i<=N;i++){
cin>>p[i];
}
memset(vis,0,sizeof vis);
vec.clear();
for(int i=1;i<=N;i++){
if(!vis[i]) ori=i,cnt=0,vec.push_back(find(ori));
}
sort(vec.begin(),vec.end());
int ans = 0;
if(vec[0]==1) ans = 1;
else if(vec[0]==2){
ans = 1;
for(int i=0;i<vec.size();i++){
if(vec[i]%2) ans = 0;
}
}
else ans = 0;
if(ans) puts("Yes");
else puts("No");
}
}
return 0;
}
转载:https://blog.csdn.net/z13653662052/article/details/101310222
查看评论