rendered paste body#include <stdio.h>
#include <stdlib.h>
#include <string.h>
unsigned long offices[1679617];
struct stos
{
unsigned long value;
struct stos* before;
};
struct stos *stack;
void push(unsigned long val)
{
struct stos *tmp = (struct stos*)malloc(sizeof(struct stos));
tmp->value = val;
tmp->before = stack;
stack = tmp;
}
unsigned long pop()
{
unsigned long t = stack->value;
struct stos *tmp = stack->before;
free(stack);
stack = tmp;
return t;
}
struct recruts
{
int pesel;
int code;
};
int compare(const void *a, const void *b)
{
struct recruts* r1 = (struct recruts*) a;
struct recruts* r2 = (struct recruts*) b;
if(r1->pesel > r2->pesel)
return 1;
else if(r1->pesel < r2->pesel)
return -1;
else
return 0;
}
int main()
{
int s, p, z, i;
char pesel[12];
char mru[5];
char to[5];
unsigned long temp;
int office;
struct recruts boys[100000];
struct recruts *r;
scanf("%d", &s);
while(s--)
{
memset(offices, 0, sizeof(unsigned long) * 1679617);
scanf("%d", &p);
for(i=0; i<p; i++)
{
scanf("%s %s", &pesel[0], &mru[0]);
boys[i].pesel = (int)strtoul(pesel + 2, NULL, 10);
boys[i].code = (int)strtoul(mru, NULL, 36);
}
qsort(boys, p, sizeof(struct recruts), compare);
scanf("%d", &z);
while(z--)
{
scanf("%s %s", &mru[0], &to[0]);
offices[(int)strtoul(mru, NULL, 36)] = strtoul(to, NULL, 36);
}
scanf("%d", &z);
while(z--)
{
scanf("%s", &pesel[0]);
temp = strtoul(pesel + 2, NULL, 10);
r = bsearch(&temp, boys, p, sizeof(struct recruts), compare);
office = r->code;
stack = (struct stos*)malloc(sizeof(struct stos));
stack->before = NULL;
stack->value = office;
while(offices[office] != 0)
{
office = offices[office];
push(office);
}
pop();
int tmp;
while(stack!=NULL)
{
tmp = pop();
offices[tmp] = office;
}
free(stack);
mru[3] = 48 + office % 36;
office /= 36;
mru[2] = 55 + office % 36;
office /= 36;
mru[1] = 55 + office % 36;
office /= 36;
mru[0] = 55 + office % 36;
printf("%s %s\n", pesel, mru);
}
printf("\n");
}
return 0;
}