Cod sursa(job #1416014)

Utilizator morandy14Andreea morandy14 Data 7 aprilie 2015 08:43:48
Problema Stramosi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.65 kb
#include<iostream>
#include<fstream>
using namespace std;
ifstream fin ("stramosi.in");
ofstream fout ("stramosi.out");

    const int kMaxN = 250005, kMaxLg = 18;

int N, M;
int str[kMaxLg][kMaxN];

int main()
{
    fin >> N >> M;
    for (int i = 1; i <= N; ++i)
        fin >> str[0][i];
    for (int i = 1; i < kMaxLg; ++i)
        for (int j = 1; j <= N; ++j)
            str[i][j] = str[i - 1][str[i - 1][j]];
    while (M--) {
        int nod, s;
        fin >> nod >> s;
        for (int i = 0; i < kMaxLg; ++i)
            if (s & (1 << i))
                nod = str[i][nod];
        fout << nod << "\n";
    }
    return 0;
}