Cod sursa(job #95617)

Utilizator MciprianMMciprianM MciprianM Data 29 octombrie 2007 18:12:45
Problema Stramosi Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.57 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 i=1,s=a[q];
  while(i<p){
    if(s<=n && s!=0){ s=a[s];i++;}
    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,q);  
    g<<s<<endl;
  }
  g<<endl;
  f.close();
  g.close();
  return 0;
}