Pagini recente » Cod sursa (job #154339) | Cod sursa (job #1163165) | Cod sursa (job #714071) | Cod sursa (job #2628990) | Cod sursa (job #2398812)
#include <iostream>
#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() {
FILE *fin, *fout ;
fin = fopen ("stramosi.in", "r" ) ;
fout = fopen ("stramosi.out", "w" ) ;
int n, m, i, v, x;
fscanf (fin, "%d%d", &n, &m ) ;
for (i = 1 ; i <= n ; i++ )
fscanf (fin, "%d", &father[i][0] ) ;
preput () ;
precalc(n) ;
for (i = 1 ; i <= m ; i++) {
fscanf (fin, "%d%d", &v, &x ) ;
fprintf (fout, "%d\n", fadar(v, x) ) ;
}
return 0;
}