Cod sursa(job #2755268)

Utilizator FraNNNkieFrancesco-Gregorio Petrovici FraNNNkie Data 26 mai 2021 22:25:33
Problema Stramosi Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.73 kb
#include <fstream>
#include <vector>

using namespace std;

int main()
{
    ifstream f("stramosi.in");
    ofstream g("stramosi.out");
    int nod, index, n, m;
    f >> n >> m;
    vector <int> stramosi(n + 1);
    for(int i = 1; i <= n; ++i){
        f >> stramosi[i];
    }

    for(int i = 1; i <= m; ++i)
    {
        f >> nod >> index;
        int stramos = nod;
        while(index && stramos != 0){
            index --;
            stramos = stramosi[stramos];
            //mergem pe vectorul de tati
        }
        if(index != 0){
            //daca nu exista al x-ulea mostenitor
            g << 0 << "\n";
        }
        else{
            g << stramos << "\n";
        }
    }
    return 0;
}