比赛 2026.3.14 评测结果 WWWWWWWWWWWWWWWWWWWW
题目名称 The Chase 最终得分 0
用户昵称 汐汐很希希 运行时间 10.541 s
代码语言 C++ 内存使用 5.35 MiB
提交时间 2026-03-14 11:56:47
显示代码纯文本
#include<bits/stdc++.h>
#define ll long long
#define mk make_pair
#define fi first
#define se second
#define debug printf("ciallo\n");
const int N=5e5+10;
const int M=2e5+10;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;
using namespace std;
int n,f,a[N],far[N];
bool ifar[N];
int main()
{
	freopen("Chase.in","r",stdin);
	freopen("Chase.out","w",stdout);
	
	cin>>n>>f;
	for(int i=1;i<=n;++i) cin>>a[i];
	for(int i=0;i<f;++i){
		cin>>far[i];
		ifar[far[i]]=true;
	}
	for(int i=1;i<=n;++i){
		if(ifar[i]){
			cout<<-1<<endl;
			continue;
		}
		int maxk=-1;
		for(int k=0;k<=2*n;k++){
			bool flag=true;
			for(int t=0;t<=2*n;++t){
				int pos;
				if(t<k){
					pos=i;
				}else{
					int steps=t-k;
					int cur=i;
					for(int s=0;s<steps;++s) cur=a[cur];
					pos=cur;
				}
				bool fflag=false;
				for(int idx=0;idx<f;++idx){
					int f=far[idx];
					int cur=f;
					for(int s=0;s<t;s++) cur=a[cur];
					if(cur==pos){
						fflag=true;
						break;
					}
				}
				if(fflag){
					fflag=false;
					break;
				}
			}
			if(flag) maxk=k;
		}
		if(maxk==-1) cout<<-1<<endl;
		else if(maxk==2*N) cout<<-2<<endl;
		else cout<<maxk<<endl;
	}
	return 0;
}