Pagini recente » Cod sursa (job #972984) | Cod sursa (job #2048499) | Rating Alexandru Theodor Bileca (Alexbileca) | Cod sursa (job #1201956) | Cod sursa (job #2398814)
#include <iostream>
#include <stdio.h>
#include <fstream>
#define NMAX 250000
#define MMAX 300000
#define LOGMAX 20
using namespace std;
int father [ LOGMAX + 1 ] [ NMAX + 1 ] ;
int put [ LOGMAX ] ;
inline void preput () {
int j ;
put[0] = 1 ;
for (j = 1 ; j < LOGMAX ; j++ )
put[j] = put[j-1] * 2 ;
}
inline void precalc (int n) {
int i, j;
for (i = 1 ; i <= n ; i++ )
for (j = 1 ; j < LOGMAX ; j++ )
father[j][i] = father[j-1][father[j-1][i]] ;
}
inline int fadar (int nod, int x ) {
int j ;
for (j = 0 ; put[j] <= x ; j++ ) {
if ( ( put[j] & x ) != 0 )
nod = father[j][nod] ;
}
return nod ;
}
int main() {
ifstream fin ("stramosi.in") ;
ofstream fout ("stramosi.out" ) ;
std::ios::sync_with_stdio(false) ;
int n, m, i, v, x;
fin >> n >> m ;
for (i = 1 ; i <= n ; i++ )
fin >> father[0][i] ;
preput () ;
precalc(n) ;
for (i = 1 ; i <= m ; i++) {
fin >> v >> x ;
fout << fadar(v,x) << '\n' ;
}
return 0;
}