Cod sursa(job #3134527)

Utilizator unomMirel Costel unom Data 29 mai 2023 11:39:05
Problema Stramosi Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream in("stramosi.in");
ofstream out("stramosi.out");
int n, m;
int v[250005];
vector<int> ans[250005];

int main()
{
    in>>n>>m;

    for(int i = 1; i<=n; i++)
    {
        in>>v[i];
    }

    int x, y;
    for(int i = 1; i<=n; i++)
    {
        x = v[i];
        ans[i].push_back(x);

        while(x != 0 && x != v[x])
        {
            x = v[x];
            ans[i].push_back(x);
        }
    }

    /*for(int i = 1; i<=n; i++)
    {
        for(auto j : ans[i])
        {
            out<<j<<" ";
        }
        out<<'\n';
    }*/

    while(m--)
    {
        in>>x>>y;
        y--;

        if(y >= ans[x].size())
        {
            out<<0<<'\n';
        }
        else
        {
            out<<ans[x][y]<<'\n';
        }
    }

    return 0;
}