比赛 |
2025.9.13 |
评测结果 |
AAAAAAAAAAAAAAAAAA |
题目名称 |
Vocabulary Quiz |
最终得分 |
100 |
用户昵称 |
hsl_beat |
运行时间 |
14.964 s |
代码语言 |
C++ |
内存使用 |
56.55 MiB |
提交时间 |
2025-09-13 11:28:41 |
显示代码纯文本
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, cntt, fa[1111111], dep[1111111], cnt[1111111];
vector<int> edges[1111111];
void dfs1(int x)
{
dep[x] = dep[fa[x]] + 1;
cntt += (!cnt[x]);
for (auto nex : edges[x]) {
dfs1(nex);
}
}
int dfs2(int x)
{
if (cnt[x]) {
return dep[x];
}
if (x == 0) {
return 0;
}
cnt[fa[x]]--;
return dfs2(fa[x]);
}
signed main()
{
freopen("Vocabulary.in", "r", stdin);
freopen("Vocabulary.out", "w", stdout);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> fa[i];
cnt[fa[i]]++;
edges[fa[i]].push_back(i);
}
dfs1(0);
while (cntt--) {
int x;
cin >> x;
cout << dfs2(x) << '\n';
}
return 0;
}