Cod sursa(job #2010385)
| Utilizator | Data | 12 august 2017 20:47:27 | |
|---|---|---|---|
| Problema | Stramosi | Scor | 70 |
| Compilator | cpp | Status | done |
| Runda | Arhiva de probleme | Marime | 0.57 kb |
#include <iostream>
#include <fstream>
#define nmax 250005
using namespace std;
ifstream f("stramosi.in");
ofstream g("stramosi.out");
long long ancestor[nmax], n;
int stramos(int p, int q)
{
if(ancestor[q]==0) return 0;
else
{
--p;
if(p==0) return ancestor[q];
else return stramos(p, ancestor[q]);
}
}
int main()
{
int m, q, p;
f>>n>>m;
for(int i=1; i<=n; ++i)
{
f>>ancestor[i];
}
while(m--)
{
f>>q>>p;
g<<stramos(p, q)<<"\n";
}
return 0;
}
