Pagini recente » Cod sursa (job #3346868) | Cod sursa (job #2037951) | Cod sursa (job #230679) | Cod sursa (job #3261665) | Cod sursa (job #3310642)
#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;
}