Cod sursa(job #2811639)

Utilizator cdenisCovei Denis cdenis Data 2 decembrie 2021 19:28:11
Problema Stramosi Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <deque>
#include <stdio.h>
#include <ctype.h>

class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;

	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
		return buff[sp];
	}

public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}

	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
			n = c - '0';
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		return *this;
	}
};

using namespace std;

int n,m,v[250005],q,p;

int main()
{
    InParser fin("stramosi.in");
    ofstream fout("stramosi.out");
    fin >> n >> m;
    for(int i=1;i<=n;i++)
        fin >> v[i];
    for(;m;--m)
    {
        fin >> q >> p;
        while(p && v[q])
        {
            q=v[q];
            p--;
        }
        fout << (p?0:q) << '\n';
    }
    return 0;
}