Cod sursa(job #2503355)

Utilizator Iulia25Hosu Iulia Iulia25 Data 2 decembrie 2019 21:46:47
Problema Stramosi Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.62 kb
#include <fstream>

using namespace std;

int n, m, x, y, k;
int tata[25][250005];

int main()	{
	ifstream fin ("stramosi.in");
	ofstream fout ("stramosi.out");

	ios_base::sync_with_stdio(false);
	fin.tie(0);
	fout.tie(0);

  fin >> n >> m;
  for (int i = 1; i <= n; ++i)
		fin >> tata[1][i];
	bool gata = false;
	for (int i = 1; !gata; ++i)	 {
		gata = true;
		for (int j = 1; j <= n; ++j)	{
			tata[i + 1][j] = tata[i][tata[i][j]];
			if (tata[i + 1][j])
				gata = false;
		}
	}
	while (m--)	 {
    fin >> x >> y;
    for (int i = 0; i <= 19; ++i)
			if (y & (1 << i))
				x = tata[i + 1][x];
    fout << x << '\n';
	}
	return 0;
}