Codeforces 1006E
Posted tiberius
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 1006E相关的知识,希望对你有一定的参考价值。
#include<bits/stdc++.h> using namespace std; const int maxn = 200005; int dfn[maxn],rdfn[maxn],children[maxn]; vector<int> graph[maxn]; int n,q,cnt; int dfs(int u){ int ret = 1; dfn[u] = cnt; rdfn[cnt++] = u; for(int i = 0;i < graph[u].size();++i){ int to = graph[u][i]; ret += dfs(to); } return children[u] = ret; } int main(){ scanf("%d%d",&n,&q); int temp; for(int i = 2;i <= n;++i){ scanf("%d",&temp); graph[temp].push_back(i); } children[1] = dfs(1); int v,numb; for(int i = 0;i < q;++i){ scanf("%d%d",&v,&numb); if(children[v] < numb) printf("-1 "); else printf("%d ",rdfn[dfn[v] + numb - 1]); } return 0; }
dfs序的使用。。。。
以上是关于Codeforces 1006E的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces 86C Genetic engineering(AC自动机+DP)
CodeForces 1005D Polycarp and Div 3(思维贪心dp)
(Incomplete) Codeforces 394 (Div 2 only)