Pagini recente » Cod sursa (job #693678) | Cod sursa (job #769677)
Cod sursa(job #769677)
#include <iostream>
#include <fstream>
using namespace std;
#define nmax 250005
ifstream f("stramosi.in");
ofstream g("stramosi.out");
int n, m, t[nmax], a[nmax][19];
void citeste(){
f >> n >> m;
for(int i=1; i<=n; i++){
int x;
f >> x;
t[i] = x;
int nod = i;
for(int j=1; j<=18; j++){
a[i][j] = t[nod];
nod = t[nod];
}
}
}
void rezolva(){
for(int i=1; i<=m; i++){
int x, y;
f >> x >> y;
int cat = y /18;
int nod = x;
for(int j=1; j<=cat; j++){
nod = a[nod][18];
}
int rest = y % 18;
if (rest>0) nod = a[nod][rest];
g << nod << "\n";
}
}
int main(){
citeste();
rezolva();
f.close();
g.close();
return 0;
}