Pagini recente » Cod sursa (job #2903948) | Cod sursa (job #2020018) | Cod sursa (job #195324) | Cod sursa (job #1294852) | Cod sursa (job #144785)
Cod sursa(job #144785)
#include <cstdio>
#include <cstdlib>
int n, m;
struct lista
{
struct nod * nod;
struct lista * next;
};
struct nod
{
int id;
int P;
int Pi;
struct lista * fii;
struct lista * praslea;
};
struct nivel
{
struct nod * nod;
struct lista * fiu;
};
struct nod nds[250010];
struct nivel stiva[250010];
int Q[250010];
int Qi = 0;
int main()
{
freopen("stramosi.in", "r", stdin);
freopen("stramosi.out", "w", stdout);
int i, q, p, nivel;
struct lista * c_lista;
struct nod * c_nod;
scanf("%d%d", &n, &m);
for(i = 1; i <= n; i ++)
{
scanf("%d", &p);
if(nds[p].fii == NULL)
{
nds[p].id = p;
nds[p].fii = (lista *) malloc(sizeof(struct lista));
c_nod = &nds[i];
c_nod->id = i;
c_nod->fii = NULL;
c_nod->praslea = NULL;
nds[p].fii->nod = c_nod;
nds[p].fii->next = NULL;
nds[p].praslea = nds[p].fii;
}
else
{
c_nod = &nds[i];
c_nod->id = i;
c_nod->fii = NULL;
c_nod->praslea = NULL;
c_lista = (lista *) malloc(sizeof(struct lista));
c_lista->next = NULL;
c_lista->nod = c_nod;
nds[p].praslea->next = c_lista;
nds[p].praslea = c_lista;
}
}
for(i=0; i < m; i++)
{
scanf("%d%d", &q, &p);
nds[q].P = p;
nds[q].Pi = i;
}
stiva[0].nod = &nds[0];
stiva[0].fiu = stiva[0].nod->fii;
nivel = 0;
do
{
if(stiva[nivel].fiu != NULL)
{
stiva[nivel+1].nod = stiva[nivel].fiu->nod;
stiva[nivel+1].fiu = stiva[nivel+1].nod->fii;
stiva[nivel].fiu = stiva[nivel].fiu->next;
nivel++;
// GOD's question
if(stiva[nivel].nod->P != 0)
{
if(nivel-stiva[nivel].nod->P+1 > 0)
{
// printf("%d\n", stiva[(nivel-stiva[nivel].nod->P)].nod->id);
Q[stiva[nivel].nod->Pi] = stiva[(nivel-stiva[nivel].nod->P)].nod->id;
}
else
{
// printf("0\n");
Q[stiva[nivel].nod->Pi] = 0;
}
}
}
else
{
nivel--;
}
} while(nivel >= 0);
for(i=0; i<m; i++)
{
printf("%d\n", Q[i]);
}
// printf("%d\n", sizeof(nds));
return 0;
}