Cod sursa(job #2010703)

Utilizator sfechisalin@yahoo.comSfechis Alin [email protected] Data 14 august 2017 05:33:50
Problema Stramosi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.73 kb
#include <bits/stdc++.h>

#define oo 0x3f3f3f3
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");

int N, M;
const int LEVEL = 20;
int father[20][250005];

void precomputeSparseMatrix()
{
   for (int lev = 1; lev <= LEVEL; ++lev)
        for (int j = 1; j <= N; ++j)
            father[lev][j] = father[lev - 1][father[lev - 1][j]];
}

int main()
{
    fin >> N >> M;

    for(int i = 1; i <= N; ++i)
        fin >> father[0][i];

    precomputeSparseMatrix();


    for(int P, Q; M; --M)
    {
        fin >> Q >> P;

        for (int i = 0; (1 << i) <= P; ++i)
            if (P & (1 << i))
                Q = father[i][Q];

        fout << Q << "\n";
    }

    return 0;
}