Cod sursa(job #1438395)

Utilizator VladutZ94FMI Chichirau Vlad Vasile VladutZ94 Data 19 mai 2015 20:36:12
Problema Stramosi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <iostream>
#include <fstream>
#include <vector>
#define MAX_N 250001
#define MAX_LOG 19

int n, m;
int stramosi[MAX_LOG][MAX_N];

int getAnc(int anc, int node)
{
	int currAnc = node;
	for (int i = 0; (1 << i) <= anc; i++)
	{
		if (anc&(1 << i))
		{
			currAnc = stramosi[i][currAnc];
		}
	}
	return currAnc;
}

int main()
{
	std::ifstream f("stramosi.in");
	int x;
	f >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		f >> x;
		stramosi[0][i] = x;
	}

	for (int i = 1; i <= MAX_LOG; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			stramosi[i][j] = stramosi[i - 1][stramosi[i - 1][j]];
		}
	}

	std::ofstream g("stramosi.out");

	int node, anc;
	for (int i = 1; i <= m; i++)
	{
		f >> node>> anc;
		g <<getAnc(anc, node)<< "\n";
	}
	return 0;
}