Cod sursa(job #1235619)

Utilizator Vally77FMI Calinescu Valentin Gelu Vally77 Data 30 septembrie 2014 03:26:54
Problema Stramosi Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.62 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream ka("stramosi.in");
ofstream ki("stramosi.out");

const int N_MAX = 250000;
const int LOG_MAX = 18;

int stramos[N_MAX + 1][LOG_MAX + 1];


int n, m, p, q;
int main()
{
    ka >> n >> m;
    for(int i = 1; i <= n; i++)
        ka >> stramos[i][0];
    for(int j = 1; j <= LOG_MAX; j++)
        for(int i = 1; i <= n; i++)
            stramos[i][j] = stramos[stramos[i][j-1]][j-1];
    while(m--)
    {
        ka >> q >> p;
        for(int i = 18; i >= 0; i--)
        if(p & (1 << i))
        {
            q = stramos[q][i];
        }
        ki << q << '\n';
    }
}