Cod sursa(job #90986)

Utilizator pitbullpitbulll pitbull Data 10 octombrie 2007 22:54:48
Problema Stramosi Scor 80
Compilator c Status done
Runda Arhiva de probleme Marime 0.67 kb
# include <stdio.h>
# define NMAX 250000
int N,M;
int v[NMAX],i;
int nod,nrstramos;

int gaseste_stramos(int,int);

int main(){
	FILE *in=fopen("stramosi.in","r");
	FILE *out=fopen("stramosi.out","w");
	fscanf(in,"%d %d",&N,&M);
	for (i=1;i<=N;i++)
		fscanf(in,"%d",&v[i]);
	for (i=0;i<M;i++){
		fscanf(in,"%d %d",&nod,&nrstramos);
		int nr=gaseste_stramos(nod,nrstramos);
		fprintf(out,"%d\n",nr);
	}
	fclose(in);
	fclose(out);
	return 0;
}

int gaseste_stramos(int nod,int nrstramos){
	if(nrstramos==0)
		return nod;//nu se doreste stramosul,returnez nodul
	else
		if(v[nod]==0)
			return 0;
		else 	
			return gaseste_stramos(v[nod],nrstramos-1);
		
}