Cod sursa(job #2401497)

Utilizator soverysourSebastian soverysour Data 9 aprilie 2019 19:23:55
Problema Stramosi Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.56 kb
#include <fstream>
#include <unordered_map>
#include <vector>
using namespace std;

#define swap(x, y) ( {auto _x = x; x = y; y = _x;} )

int main()
{
  ifstream f{"stramosi.in"};
  ofstream g{"stramosi.out"};

  int n, q;
  f >> n >> q;

  vector<int> fathers;

  int aux;
  for ( int i = 0; i < n; i++ ) {
    f >> aux;
    aux--;

    fathers.push_back(aux);
  }


  int om, nlea;
  for ( int i = 0; i < q; i++ ) {
    f >> om >> nlea;
    om--;

    while ( nlea > 0 && om > -1 ) {
      om = fathers[om];
      nlea--;
    }

    g << om + 1 << endl;
  }

  return 0;
}