标签:++ space dfs序 rap for def else children printf
#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\n"); else printf("%d\n",rdfn[dfn[v] + numb - 1]); } return 0; }
dfs序的使用。。。。
标签:++ space dfs序 rap for def else children printf
原文地址:https://www.cnblogs.com/tiberius/p/9347997.html