Cod sursa(job #3310642)

Utilizator Andrei-Dani-10Pisla Andrei Daniel Andrei-Dani-10 Data 15 septembrie 2025 17:59:31
Problema Stramosi Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <fstream>

#include <vector>

using namespace std;

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

const int nmax = 250000, lgmax = 18;
int n, nrq, node, topp, father[nmax + 2];
vector <int> revmuchii[nmax + 2];

int stk[nmax + 2];
void dfs(int node){

    stk[++stk[0]] = node;

    ///out<<"For: "<<node<<" -> ";
    ///for(int it = 1; it <= stk[0]; it++)
    ///    out<<stk[it]<<" "; out<<"\n";

    for(auto nxt : revmuchii[node]){
        dfs(nxt);
    }

    stk[stk[0]--] = 0;
}

int main(){

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

        revmuchii[father[i]].push_back(i);
    }

    for(int i = 1; i <= n; i++){
        if(!father[i]){ dfs(i); }
    }

    for(int i = 1; i <= nrq; i++){
        in>>node>>topp;

        out<<node<<"\n";
    }

    return 0;
}