Cod sursa(job #2398814)

Utilizator alex.cojocaruAlex Cojocaru alex.cojocaru Data 6 aprilie 2019 10:50:40
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb
#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;
}