Pagini recente » Cod sursa (job #1370112) | Cod sursa (job #2389582) | Cod sursa (job #2283545) | Cod sursa (job #3210107) | Cod sursa (job #2745971)
//Ilie Dumitru
#include<cstdio>
#define NMAX 32001
int tati[NMAX], cost[NMAX], depth[NMAX];
int lca(int x, int y)
{
int min=100001;
if(x==y)
return 0;
while(depth[x]<depth[y])
{
if(cost[y]<min) min=cost[y];
y=tati[y];
}
while(depth[x]>depth[y])
{
if(cost[x]<min) min=cost[x];
x=tati[x];
}
while(x!=y)
{
if(cost[x]<min) min=cost[x];
if(cost[y]<min) min=cost[y];
x=tati[x];
y=tati[y];
}
return min;
}
int main()
{
freopen("atac.in", "r", stdin);
freopen("atac.out", "w", stdout);
int N, P, M, i, x, A, B, C, D, X, Y, Z;
scanf("%d%d%d", &N, &M, &P);
for(i=1;i<N;++i) {scanf("%d%d", &x, &cost[i]);tati[i]=--x;depth[i]=depth[x]+1;}
scanf("%d%d%d%d%d%d", &X, &Y, &A, &B, &C, &D);
fclose(stdin);
--X;
--Y;
for(i=M;i>-1;i--)
{
Z=lca(X, Y);
X=((X+1)*A+(Y+1)*B)%N;
Y=((Y+1)*C+Z*D)%N;
if(i<P)
printf("%d\n", Z);
}
fclose(stdout);
return 0;
}