Cod sursa(job #1344922)

Utilizator Vally77FMI Calinescu Valentin Gelu Vally77 Data 17 februarie 2015 08:51:30
Problema Stramosi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.71 kb
#include <fstream>
#include <stdio.h>
using namespace std;
ofstream ki("stramosi.out");

const int N_MAX = 250000;
const int LOG_MAX = 17;

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

int n, m, p, q, f;

int main()
{
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++)
        scanf("%d", &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];
    for(int i = 1; i <= m; i++)
    {
        scanf("%d%d", &q, &p);
        int f = 0;
        while(p)
        {
            if(p % 2 != 0)
                q = stramos[q][f];
            f++;
            p >>= 1;
        }
        ki << q << '\n';
    }
}