题解 | 谍中谍中谍中谍中谍...

谍中谍中谍中谍中谍...

https://www.nowcoder.com/practice/ee1246384c9b4066b67043ebb37fd9c9

#include<bits/stdc++.h>
using namespace std;

const int N=1e3+10;

int n,p[N]; 

void solve(){
	
	for(int i=1;i<=n;i++){
		
		vector<bool> vis(n+1,false);
		
		vis[i]=true;
		int tp=i;
		while(true){
			
			tp=p[tp];
			if(vis[tp]==true){
				cout<<tp<<" ";
				break;
			}
			vis[tp]=true;
			
		}
		
		
	}
	
	return;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>p[i];
	}
	
	solve();

    return 0;
}


全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务