Cod sursa(job #2206134)

Utilizator memecoinMeme Coin memecoin Data 21 mai 2018 12:43:33
Problema Stramosi Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.66 kb
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
 
using namespace std;

#define MAXN 250005
#define MAXLOGN 20

int n, m;

int parents[MAXLOGN][MAXN];

int query(int x, int depth) {
	if (x == 0) {
		return 0;
	}
	if (depth == 0) {
		return x;
	}
	return query(parents[0][x], depth - 1);
}

int main() {
	freopen("stramosi.in", "r", stdin);
	freopen("stramosi.out", "w", stdout);

	scanf("%d %d", &n, &m);

	for (int i = 1; i <= n; ++i) {
		scanf("%d", &parents[0][i]);
	}

	int x, depth;

	for (int i = 0; i < m; ++i) {
		scanf("%d %d", &x, &depth);
		printf("%d\n", query(x, depth));
	}

	return 0;
}