Cod sursa(job #769677)

Utilizator vendettaSalajan Razvan vendetta Data 20 iulie 2012 15:03:12
Problema Stramosi Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <iostream>
#include <fstream>

using namespace std;

#define nmax 250005

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

int n, m, t[nmax], a[nmax][19];

void citeste(){

    f >> n >> m;

    for(int i=1; i<=n; i++){
        int x;
        f >> x;
        t[i] = x;
        int nod = i;
        for(int j=1; j<=18; j++){
            a[i][j] = t[nod];
            nod = t[nod];
        }
    }

}

void rezolva(){

    for(int i=1; i<=m; i++){
        int x, y;
        f >> x >> y;
        int cat = y /18;
        int nod = x;
        for(int j=1; j<=cat; j++){
            nod = a[nod][18];
        }
        int rest = y % 18;
        if (rest>0) nod = a[nod][rest];
        g << nod << "\n";
    }

}

int main(){

    citeste();
    rezolva();

    f.close();
    g.close();

    return 0;

}