Cod sursa(job #2763170)

Utilizator Bulboaca_EugenBulboaca Alexandru Eugen Bulboaca_Eugen Data 12 iulie 2021 10:10:34
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <bits/stdc++.h>
#define zeros(x) x & (-x)

using namespace std;

const int MAXN = 25 * 1e4 + 65;
const int INF = 1e8;

ifstream fin("stramosi.in");
ofstream fout("stramosi.out");

int father[24][MAXN];

void stramosi(int y, int x){
    int ans = y;
    for(int i = 0; i <= 23; ++i){
        if((1 << i) & x)
            ans = father[i][ans];
    }
    fout << ans << '\n';
}

int main(){

    int n, m; fin >> n >> m;
    for(int i = 1; i <= n; ++i){
        fin >> father[0][i];
    }
    for(int i = 1; i <= 21; ++i){
        for(int j = 1; j <= n; j ++){
            father[i][j] = father[i - 1][father[i - 1][j]];
        }
    }

    for(int i = 1; i <= m; ++i){
        int x, y; fin >> x >> y;

        stramosi(x, y);
    }
    return 0;
}