Cod sursa(job #1978741)

Utilizator RaresEGaySopterean Adrian RaresEGay Data 8 mai 2017 18:29:02
Problema Stramosi Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.52 kb
#include <fstream>
#include <iostream>

#define MAXN 250005

using namespace std;

ifstream f ("stramosi.in");
ofstream g ("stramosi.out");

int n, m, p[MAXN];

int main(){
    f >> n >> m;
    for(int i = 1; i <= n; ++i){
        int x;
        f >> x;
        p[i] = x;
    }
    for(int i = 1; i <= m; ++i){
        int k = 0, x, y;
        f >> y >> x;
        while(k != x && y != 0){
            ++k; y = p[y];
        }
        if(k != x) g << 0 << '\n';
        else g << y << '\n';
    }
}