Cod sursa(job #369681)

Utilizator Bogdan_CCebere Bogdan Bogdan_C Data 29 noiembrie 2009 10:22:31
Problema Stramosi Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.58 kb
#include<cstdio>
using namespace std;
#define FIN "stramosi.in"
#define FOUT "stramosi.out"
int stramos[250001],niv[250001],n,m,x,y,aux,pos;
int fct(int a)
{ while(y!=0 && a!=0)
  {y--; a=stramos[a];
            } 
    return a;        
  }



int main()
{freopen(FIN,"r",stdin);
freopen(FOUT,"w",stdout);
scanf("%d %d",&n,&m);  
  niv[0]=0;stramos[0]=0;
for(int i=1;i<=n;i++) niv[i]=stramos[i]=0;
for(int i=1;i<=n;i++)
 {scanf("%d",&stramos[i]);niv[i]=niv[stramos[i]]+1;

    }
for(;m;m--)
{scanf("%d %d",&x,&y); printf("%d \n",fct(x));  }



      return 0;
}