Cod sursa(job #3042265)

Utilizator SSKMFSS KMF SSKMF Data 5 aprilie 2023 08:58:20
Problema Stramosi Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.42 kb
#include <fstream>
#include <bitset>
#include <vector>
using namespace std;

ifstream cin ("stramosi.in");
ofstream cout ("stramosi.out");

bitset <250001> ascendent , parcurs;
vector < vector <int> > ascendenti;
vector <int> tati;

void Parcurgere (int nod , vector <int> &descendenti)
{
    if (nod == 0)
        return;

    for (auto descendent : descendenti)
        ascendenti[descendent].push_back(nod);

    if (!parcurs[nod])
        descendenti.push_back(nod) , parcurs[nod] = 1;

    Parcurgere(tati[nod] , descendenti);
}

int main ()
{
    int lungime , intrebari;
    cin >> lungime >> intrebari;

    tati.resize(lungime + 1) , ascendenti.resize(lungime + 1);

    for (int indice = 1 ; indice <= lungime ; indice++)
        cin >> tati[indice] , ascendent[tati[indice]] = 1;

    for (int indice = 1 ; indice <= lungime ; indice++)
        if (!ascendent[indice])
        {
            vector <int> descendenti;
            descendenti.push_back(indice) , parcurs[indice] = 1;
            Parcurgere(tati[indice] , descendenti);
        }

    int membru , stramos;
    for (int indice = 1 ; indice <= intrebari ; indice++)
    {
        cin >> membru >> stramos;

        if (ascendenti[membru].size() < (unsigned)stramos)
            cout << 0 << '\n';
        else
            cout << ascendenti[membru][stramos - 1] << '\n';
    }

    cout.close(); cin.close();
    return 0;
}