Cod sursa(job #197286)

Utilizator cristitalauCristian Talau cristitalau Data 3 iulie 2008 10:36:53
Problema Stramosi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <stdio.h>
#define QMAX 300005
#define NMAX 250005
#define LOG 20
int M, N;
int P[NMAX][LOG]; // P[a][b] al 2^b-lea parinte a lui a.

int main(void)
{
	int i , j , s, tmp,  parinte, nod, exp;
	freopen("stramosi.in", "r", stdin);
	freopen("stramosi.out", "w", stdout);
	
	scanf("%d %d", &N, &M);
	for( i = 0; i< N; i++){
		scanf("%d", &P[i+1][0]);
	}
	for( j = 1; j < LOG; j++)
		for( i = 1; i < N+1; i++)
			P[i][j] = P [ P[i][j-1]][j-1];
	
	for( i = 0; i < M; i++){
		scanf("%d %d ", &nod, &parinte);
		while(parinte){
			for (tmp = 1, exp = 0; tmp < s; tmp <<=1, ++exp);
			nod = P[nod][exp];
			parinte  = parinte - tmp;
		}			
		printf("%d\n", nod);
	}	
	
	return 0;
}