Pagini recente » Cod sursa (job #2028775) | Cod sursa (job #1307057) | Cod sursa (job #1069723) | Cod sursa (job #1754410) | Cod sursa (job #90980)
Cod sursa(job #90980)
# 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
if(nrstramos==1)
return v[nod];
else
if(v[nod]==0)
return 0;
else
return gaseste_stramos(v[nod],nrstramos-1);
}