Pagini recente » Cod sursa (job #749664) | Cod sursa (job #1563976) | Cod sursa (job #717235) | Cod sursa (job #1614284) | Cod sursa (job #1588181)
#include <iostream>
#include <cstdio>
#define MAXN 33000
#define MAXLOG 20
#define inf 0x7fffffff
using namespace std;
int n, m, p;
int t[MAXLOG][MAXN], cost[MAXLOG][MAXN];
int depth[MAXN];
int a, b, c, d, x, y, z;
void citire()
{
scanf("%d %d %d", &n, &m, &p);
for (int i = 2; i <= n; i++)
scanf("%d %d", &t[0][i], &cost[0][i]);
t[0][1] = 0; cost[0][1] = inf;
scanf("%d %d %d %d %d %d", &x, &y, &a, &b, &c, &d);
}
int getDepth(int nod)
{
if (depth[nod]) return depth[nod];
return getDepth(t[0][nod])+1;
}
void prepare()
{
for (int i = 1; (1<<i) <= n; i++) {
for (int j = 1; j <= n; j++) {
t[i][j] = t[i-1][t[i-1][j]];
cost[i][j] = min(cost[i-1][j], cost[i-1][t[i-1][j]]);
}
}
depth[1] = 1;
for (int i = 2; i <= n; i++)
depth[i] = getDepth(i);
}
void solve()
{
int n1 = x, n2 = y;
int d1 = depth[n1], d2 = getDepth(n2);
if (d2 > d1) {
swap(d1, d2);
swap(n1, n2);
}
int best = inf;
while (depth[n1] > depth[n2]) {
int step = 0, dif = depth[n1]-depth[n2];
for (step; 1<<step < dif; step++);
for (step; step >= 0; step--)
if (depth[t[step][n1]] >= depth[n2]) {
best = min(best, cost[step][n1]);
n1 = t[step][n1];
}
}
if (depth[n1] != depth[n2])
cerr << "ERROR 6-";
int step = 0;
for (step; 1<<step < n; step++);
for (step; step >= 0; step--)
if (t[step][n1] != t[step][n2]) {
best = min(best, cost[step][n1]);
best = min(best, cost[step][n2]);
n1 = t[step][n1];
n2 = t[step][n2];
}
if (n1 != n2) {
best = min(best, cost[0][n1]);
best = min(best, cost[0][n2]);
}
if (best == inf) best = 0;
z = best;
}
int main()
{
freopen("atac.in", "r", stdin);
freopen("atac.out", "w", stdout);
citire();
prepare();
for (int i = 1; i <= m; i++) {
solve();
if (i > m-p) {
///DEBUG: printf("%d %d :=> ", x, y);
printf("%d\n", z);
}
int xp = (x*a + y*b) % n + 1;
int yp = (y*c + z*d) % n + 1;
x = xp; y = yp;
}
return 0;
}