Cod sursa(job #3231618)

Utilizator alexionut05Alexandru-Ionut Tincu alexionut05 Data 27 mai 2024 13:05:11
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.63 kb
#include <fstream>

#define SIZE 250001

int main()
{
	std::ifstream fin("stramosi.in");
	std::ofstream fout("stramosi.out");

	int64_t members, queries;
	fin >> members >> queries;

	int64_t tree_depth = 0;

	int64_t ancestors[SIZE];
	for (int64_t i = 1; i <= members; ++i) {
		fin >> ancestors[i];
		tree_depth += (ancestors[i] == i - 1);
	}

	if (tree_depth == members) {
		while (queries--) {
			int64_t x, y;
			fin >> x >> y;
			fout << ((x >= y) ? (x - y) : 0) << '\n';
		}
	}

	else {
		while (queries--) {
			int64_t x, y;
			fin >> x >> y;

			while (y-- && x) {
				x = ancestors[x];
			}

			fout << x << '\n';
		}
	}

	return 0;
}