记录编号 605961 评测结果 AAAAAAAAAAAAAAAAAA
题目名称 4176.[USACO25 Feb Silver]Vocabulary Quiz 最终得分 100
用户昵称 GravatarRuyi 是否通过 通过
代码语言 C++ 运行时间 13.523 s
提交时间 2025-09-13 14:32:36 内存使用 47.38 MiB
显示代码纯文本
#include<bits/stdc++.h>
#define ll long long
#define N 1000001
using namespace std;
int n,m,p[N],dep[N],cnt[N],a[N];
vector<int> e[N];
void f1(int x){
    dep[x]=dep[p[x]]+1;
    if(!cnt[x]) m++;
    for(auto i:e[x]) f1(i);
}
int f2(int x){
    if(cnt[x]) return dep[x]+1;
    if(x==0) return 0;
    cnt[p[x]]--;
    return f2(p[x]);
}
int main(){
    freopen("Vocabulary.in","r",stdin);
    freopen("Vocabulary.out","w",stdout);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>p[i];
        cnt[p[i]]++;
        e[p[i]].push_back(i);
    }
    p[0]=0;
    dep[0]=-1;
    f1(0);
    for(int i=1;i<=m;i++) cin>>a[i];
    for(int i=1;i<=m;i++) cout<<f2(a[i])<<endl;
    return 0;
}