Cod sursa(job #3135545)

Utilizator XSZeroBurghelea Radu-Theodor XSZero Data 3 iunie 2023 17:14:58
Problema Stramosi Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.7 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("stramosi.in");
ofstream g("stramosi.out");
int a[250001];
int main()
{
    int n,m;
    f>>n>>m;
    for(int i=1;i<=n;i++)
        f>>a[i];
    while(m--)
    {
        int q,p;
        f>>q>>p;
        if(p==1) g<<a[q]<<'\n';
        else
            {
                int x=a[q];
                p--;
               // cout<<p<<'\n';
                while(p--)
                    {
                       // cout<<x<<" "<<a[x]<<" "<<p<<'\n';
                        x=a[x];
                       // cout<<x<<" "<<a[x]<<" "<<p<<'\n';
                    }
                g<<x<<'\n';
            }
    }
    return 0;
}