Pagini recente » Cod sursa (job #181651) | Cod sursa (job #1918797) | Cod sursa (job #25161) | Cod sursa (job #269595) | Cod sursa (job #2398813)
#include <iostream>
#include <stdio.h>
#include <fstream>
#define NMAX 250000
#define MMAX 300000
#define LOGMAX 20
using namespace std;
int father [ NMAX + 1 ] [ LOGMAX + 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[i][j] = father[father[i][j-1]][j-1] ;
}
inline int fadar (int nod, int x ) {
int j ;
for (j = 0 ; put[j] <= x ; j++ ) {
if ( ( put[j] & x ) != 0 )
nod = father[nod][j] ;
}
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[i][0] ;
preput () ;
precalc(n) ;
for (i = 1 ; i <= m ; i++) {
fin >> v >> x ;
fout << fadar(v,x) << '\n' ;
}
return 0;
}