Cod sursa(job #2602845)

Utilizator DragosCosmin00Tudorache Dragos-Cosmin DragosCosmin00 Data 17 aprilie 2020 23:34:33
Problema Stramosi Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.55 kb
#include<iostream>
#include<fstream>
using namespace std;
#define NMAX 250000
#define MMAX 300000
int v[NMAX];
int CautaStramos(int a, int b)
{
	int stramos = a;
	for (int i = 1; i <= b; i++)
	{
		stramos = v[stramos];
		if (stramos == 0)
			return 0;
	}
	return stramos;
}
int main()
{
	ifstream f("stramosi.in");
	ofstream g("stramosi.out");
	int n, m;
	// Citire
	f >> n >> m;
	for (int i = 1; i <= n; i++)
		f >> v[i];
	for (int i = 1; i <= m; i++)
	{
		int a, b;
		f >> a >> b;
		g << CautaStramos(a, b) << '\n';
	}
	return 0;
}