Cod sursa(job #1513710)

Utilizator CalarisPredut Denis Stefanita Calaris Data 29 octombrie 2015 21:24:29
Problema Stramosi Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include<iostream>
#include<fstream>

int main()
{
	unsigned long N, M, Q, P;
	std::ifstream f("stramosi.in");
	//std::ofstream g("stramosi.out");
    std::ofstream g;
    g.open ("stramosi.out");
	f >> N;
	f >> M;

	int *descendantlist;
	descendantlist = new int[++N];
	unsigned int x;
	for (int i = 1; i < N; ++i) {
		f >> x;
		//g<<x;//the direct descendant of family member i
		//std::cout << x << " ";
		descendantlist[i] = x;
	}
	//std::cout << std::endl;

	x = 0;
	while (x < M){
		f >> Q; //the family member
		f >> P; //the number of the descendant searched
		x++;
		int nr = 1;
		while (nr <= P){
			Q = descendantlist[Q];
			if (Q == 0){
				//std::cout << Q << " ";
				g<<Q<<std::endl;
				break;
			}
			nr++;
		}
		if (Q!=0){
                //std::cout << Q<< " ";
                g<<Q<<std::endl;
		}
	}
	//std::cin >> x;
	return 0;
}