Pagini recente » Cod sursa (job #1002132) | Cod sursa (job #1011067) | Cod sursa (job #2639015) | Cod sursa (job #2475512) | Cod sursa (job #2503355)
#include <fstream>
using namespace std;
int n, m, x, y, k;
int tata[25][250005];
int main() {
ifstream fin ("stramosi.in");
ofstream fout ("stramosi.out");
ios_base::sync_with_stdio(false);
fin.tie(0);
fout.tie(0);
fin >> n >> m;
for (int i = 1; i <= n; ++i)
fin >> tata[1][i];
bool gata = false;
for (int i = 1; !gata; ++i) {
gata = true;
for (int j = 1; j <= n; ++j) {
tata[i + 1][j] = tata[i][tata[i][j]];
if (tata[i + 1][j])
gata = false;
}
}
while (m--) {
fin >> x >> y;
for (int i = 0; i <= 19; ++i)
if (y & (1 << i))
x = tata[i + 1][x];
fout << x << '\n';
}
return 0;
}