题面

先粘上gouzhi的题面

题解

处理出置换中的所有轮换,瞎jb打就OK了

代码

#include<cstdio>
#include<vector> 
#include<algorithm> 
const int maxn = 500007;
using std::vector;
inline int read() { 
	int x = 0; 
	char c = getchar(); 
	while(c < '0' || c > '9') c = getchar(); 
	while(c <= '9' && c >= '0')x = x * 10 + c - '0',c = getchar();
	return x; 
}
int a[maxn],bel[maxn],loc[maxn],num = 0,cnt; bool hav[maxn],vis[maxn];  
vector<int>vec[maxn]; 
void dfs(int tx,int fa) { 
	loc[tx] = ++num; 
	vec[cnt].push_back(tx); bel[tx] = cnt; 
	if(a[tx] == fa) return; 
	dfs(a[tx],fa); 
} 	
void solve(int x,int m) { 
	int B = bel[x]; 
	int size = vec[B].size(); 
	int ans_loc = (m + loc[x]) % size; 
	//if(!ans_loc)ans_loc = 1;
	printf("%d\n",vec[B][ans_loc]); 
} 
int main() { 
	freopen("kengdie.in","r",stdin); 
	freopen("kengdie.out","w",stdout); 
	int n = 0; 
	for(n = 1;;++ n) { 
		a[n] = read(); 
		if(hav[a[n]]) break;hav[a[n]] = true; 	
	} 
	for(int i = 1;i < n;++ i) { 
		//vec[i].push_back(); 
		if(!bel[i]) { 
			num = -1; bel[i] = ++ cnt; 
			dfs(i,i);   
		} 
	}	
	int x = a[n],m = read(); 	
	solve(x,m);
	while(scanf("%d%d",&x,&m) == 2) 
		solve(x,m); 
	return 0;
}  

posted @ 2018-05-11 21:06  zzzzx  阅读(271)  评论(4编辑  收藏  举报