Pagini recente » Cod sursa (job #2391850) | Cod sursa (job #2173253) | Cod sursa (job #870429) | Cod sursa (job #2560473) | Cod sursa (job #3231621)
#include <fstream>
#define SIZE 250001
int main()
{
std::ifstream fin("stramosi.in");
std::ofstream fout("stramosi.out");
int16_t members, queries;
fin >> members >> queries;
int16_t tree_depth = 0;
int16_t ancestors[SIZE];
for (int16_t i = 1; i <= members; ++i) {
fin >> ancestors[i];
tree_depth += (ancestors[i] == i - 1);
}
if (tree_depth == members) {
while (queries--) {
int16_t x, y;
fin >> x >> y;
fout << ((x >= y) ? (x - y) : 0) << '\n';
}
}
else {
while (queries--) {
int16_t x, y;
fin >> x >> y;
while (y-- && x) {
x = ancestors[x];
}
fout << x << '\n';
}
}
return 0;
}