Cod sursa(job #2662925)

Utilizator metallidethantralayerIon Cojocaru metallidethantralayer Data 24 octombrie 2020 20:41:40
Problema Stramosi Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.51 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("stramosi.in");
ofstream g("stramosi.out");
int n,m,v[250005];
vector <int> V[250005];
int main()
{
    f>>n>>m;
    for(int i=1; i<=n; i++)
    {
        f>>v[i];
        int x=v[i];
        for(; v[x]!=x;)
            V[i].push_back(x),x=v[x];
    }
    while(m--)
    {
        int x,y;
        f>>x>>y;
        y--;
        if(y>=V[x].size())
            g<<0<<'\n';
        else
            g<<V[x][y]<<'\n';
    }

    return 0;
}