Cod sursa(job #95619)

Utilizator MciprianMMciprianM MciprianM Data 29 octombrie 2007 18:16:40
Problema Stramosi Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.56 kb
#include<fstream>
using namespace std;
unsigned long int n;
unsigned long int a[250001];
unsigned long int stramos(unsigned long int p,unsigned long int q){
  unsigned long int s=a[q];
  while(p--){
    if(s<=n && s!=0) s=a[s];
    else return 0;
  }
  return s;
}
int main(){
  
  unsigned long int m,i,p,q,s;
  ifstream f("stramosi.in");
  f>>n>>m;
  for(i=1;i<=n;i++)
    f>>a[i];
  ofstream g("stramosi.out");
  for(i=1;i<=m;i++){
    f>>q>>p;
    s=stramos(p-1,q);  
    g<<s<<endl;
  }
  g<<endl;
  f.close();
  g.close();
  return 0;
}