Cod sursa(job #2695270)

Utilizator YusyBossFares Yusuf YusyBoss Data 12 ianuarie 2021 12:36:01
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <iostream>
#include <stdio.h>
#define NMAX 250000
#define LOGMAX 17

using namespace std;

int mat[LOGMAX + 1][NMAX + 1];

int getstramos(int x, int poz) { // returneaza al x-lea stramos al membrului poz
  int poz_baza2 = 0;
  while (poz) {
    if (poz % 2 == 1)
      x = mat[poz_baza2][x];
    poz_baza2++;
    poz /= 2;
  }

  return x;
}

int main() {
  FILE *fin, *fout;
  int n, m, i, j, x, y;

  fin = fopen("stramosi.in", "r");
  fscanf(fin, "%d%d", &n, &m);
  for (i = 1; i <= n; i++)
    fscanf(fin, "%d", &mat[0][i]);

  for (i = 1; (1 << i) <= n; i++)
    for (j = 1; j <= n; j++)
      mat[i][j] = mat[i - 1][mat[i - 1][j]];

  fout = fopen("stramosi.out", "w");
  while (m--) {
    fscanf(fin, "%d%d", &x, &y);
    fprintf(fout, "%d\n", getstramos(x, y));
  }
  fclose( fout );
  return 0;
}