Cod sursa(job #2243265)

Utilizator lixiLixandru Andrei lixi Data 20 septembrie 2018 10:58:16
Problema Stramosi Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.49 kb
#include <iostream>
#include<fstream>
#include<vector>
using namespace std;
ifstream f("stramosi.in");
ofstream g("stramosi.out");
vector<int> a;
int N, M, P, Q;
int main()
{
    int x;
    f >> N >> M;
    a.reserve(N + 1);
    while(N--)
    {
        f >> x;
        a.push_back(x);
    }
    while(M--)
    {
        f >> Q >> P;
        int y = a[Q - 1];
        for(int i = 1; i < P &&  y != 0; i++)
            y = a[y - 1];
        g << y << '\n';
    }
    return 0;
}