-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathatac.cpp
74 lines (63 loc) · 1.52 KB
/
atac.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# include <bits/stdc++.h>
# define NR 35000
# define mp make_pair
# define f first
# define s second
# define INF 999999999
# define DIM 17
using namespace std;
ifstream f("atac.in");
ofstream g("atac.out");
vector <pair <int, int> > v[NR];
int i,j,n,m,A,B,C,D,X,Y,Z,niv[NR],Q,P,x,cost;
int best[DIM][NR], T[DIM][NR];
void DFS (int k, int nivel) {
niv[k]=nivel;
for (int i=1; (1<<i)<=nivel; ++i) {
T[i][k]=T[i-1][T[i-1][k]];
best[i][k]=min(best[i-1][k], best[i-1][T[i-1][k]]);
}
for (auto &x: v[k]) { //daca nu am mai trecut pe aici
DFS (x.f, nivel+1);
}
}
int query (int X, int Y) {
int minim=INF;
if (X==Y) return 0;
if (niv[X] < niv[Y]) swap(X, Y);
for (int i=DIM-1; i>=0; --i)
if (niv[X] - (1<<i)>=niv[Y]) {
minim=min(minim, best[i][X]);
X=T[i][X];
}
for (int i=DIM-1; i>=0; --i) {
if (T[i][X]!=T[i][Y]) {
minim=min(minim, best[i][X]);
minim=min(minim, best[i][Y]);
X=T[i][X];
Y=T[i][Y];
}
}
if (X!=Y) {
minim=min(minim, min(best[0][X], best[0][Y]));
}
return minim;
}
int main ()
{
f>>n>>Q>>P;
for (i=2; i<=n; ++i) {
f>>x>>cost;
T[0][i]=x; best[0][i]=cost;
v[x].push_back(mp(i, cost));
}
best[0][1]=INF; DFS (1, 0);
f>>X>>Y>>A>>B>>C>>D;
for (i=1; i<=Q; ++i) {
Z=query (X, Y);
if (i>=Q - P + 1) g<<Z<<"\n";
X=(X*A + Y*B)%n + 1;
Y=(Y*C + Z*D)%n + 1;
}
return 0;
}