Pagini recente » Cod sursa (job #2221040) | Profil Wrathchild | Profil M@2Te4i | Profil M@2Te4i | Cod sursa (job #1681138)
#include <iostream>
#include <fstream>
using namespace std;
struct NodeTree
{
unsigned int info, dim = 0;
NodeTree *kids[250000];
NodeTree *father;
};
NodeTree *NodeAdress[250000];
void insert_tree(NodeTree *&root, unsigned int place, unsigned int value)
{
NodeTree *p = new NodeTree;
p->info = value;
if (root == NULL) {
p->father = NULL;
root = p;
NodeAdress[value] = p;
}
else {
p->father = NodeAdress[place];
NodeAdress[place]->kids[NodeAdress[place]->dim++] = p;
NodeAdress[value] = p;
}
}
unsigned int findAncestor(NodeTree *current, unsigned int rank, unsigned int count)
{
if (current == NULL) {
return 0;
}
if (rank == count) {
return current->info;
}
return findAncestor(current->father, rank, count + 1);
}
int main()
{
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
unsigned int n, m, x, i, dim = 0, q, p;
fin >> n >> m;
NodeTree *root[250000];
for (i = 1; i <= n; ++i) {
fin >> x;
if (x == 0) {
root[++dim] = NULL;
insert_tree(root[dim], x, i);
}
else {
insert_tree(root[dim], x, i);
}
}
while (m)
{
fin >> q >> p;
fout << findAncestor(NodeAdress[q]->father, p, 1) << '\n';
m--;
}
fin.close();
fout.close();
return 0;
}